Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hello everybody i want to know How to create dynamic control in C# with skip logic.

The Scenario is, If i click YES option then fore-more controls should come, If i click NO option then skip the question to next question.
Posted
Updated 6-Aug-13 3:41am
v2
Comments
Maarten Kools 6-Aug-13 6:13am    
Might just be me, but what is skip logic?
dhinamit 6-Aug-13 6:58am    
The Scenario is, If i click YES option then fore-more controls should come,
If i click NO option then skip the question to next question.
adriancs 6-Aug-13 6:33am    
What is skip logic?
dhinamit 6-Aug-13 6:58am    
The Scenario is, If i click YES option then fore-more controls should come,
If i click NO option then skip the question to next question.
ZurdoDev 6-Aug-13 9:52am    
Where are you stuck? If Yes is clicked, add more controls to the Form. You can do it through client side or server side.

1 solution

This might give you an idea:
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">

</head>
<body>
    <form id="form1" runat="server">
    
    <asp:Panel ID="Panel1" runat="server" EnableViewState="False" ViewStateMode="Disabled">
    </asp:Panel>
    
    <asp:button id="Button_AddControl" runat="server" text="Add Control"
        onclick="Button_AddControl_Click" />
    
    <asp:Button ID="Button_Save" runat="server" onclick="Button_Save_Click" 
        Text="Save" />
    </form>
</body>
</html>

Code behind:
C#
public partial class WebForm1 : System.Web.UI.Page
{
    List<string> lstData
    {
        get
        {
            if (ViewState["lstData"] == null)
                ViewState["lstData"] = new List<string>();
            return (List<string>)ViewState["lstData"];
        }
        set
        {
            ViewState["lstData"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            LoadControl();
        }
    }

    void LoadControl()
    {
        for (int i = Panel1.Controls.Count; i < lstData.Count; i++)
        {
            Panel p = new Panel();
            TextBox textBox = new TextBox();
            p.Controls.Add(textBox);
            Panel1.Controls.Add(p);
        }
    }

    void LoadData()
    {
        for (int i = 0; i < lstData.Count; i++)
        {
            ((TextBox)Panel1.Controls[i].Controls[0]).Text = lstData[i];
        }
    }

    void SaveData()
    {
        for (int i = 0; i < lstData.Count; i++)
        {
            lstData[i] = ((TextBox)Panel1.Controls[i].Controls[0]).Text;
        }
    }

    protected void Button_Save_Click(object sender, EventArgs e)
    {
        SaveData();
    }

    protected void Button_AddControl_Click(object sender, EventArgs e)
    {
        lstData.AddRange(new string[3]);
        LoadControl();
        LoadData();
    }
}
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900