design issue in dynamic control creation
<p>I am creating an dynamic controls I have an drop down with options   “Textbox, checkbox, dropdown, label” when user selects any of these options from the drop down controls gets created</p>

<p>Let me say I create control like these from selecting the option from the dropdown.</p>

<pre><code>1: text box 
2: drop down 
3: text box:
4: dropdown
</code></pre>

<p>However in the page output its shown like this:</p>

<pre><code>1: textbox
2: textbox
3: dropdown
4: dropdown
</code></pre>

<p>This is not the right order based on the selected order above.</p>

<p><strong>Issue here is the design is not quite right.</strong></p>

<p>When I am recreating the controls I recreate controls accoring to their types: TextBoxes, then DropDowns</p>

<p><strong>Pls find attched code  any suggestion how to solve this would be helpful.</strong></p>

<pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Dynamiccontrol.aspx.cs" Inherits="Dynamic_controls.Dynamiccontrol" %&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml" &gt;
&lt;head id="Head1" runat="server"&gt;
    &lt;title&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;form id="form1" runat="server"&gt;
    &lt;div&gt;
        &lt;asp:DropDownList ID="ddlcontrols" runat="server" AutoPostBack="True" 
            onselectedindexchanged="ddlDynamic_SelectedIndexChanged"&gt;
            &lt;asp:ListItem&gt;--Select--&lt;/asp:ListItem&gt;
            &lt;asp:ListItem&gt;Textbox&lt;/asp:ListItem&gt;
            &lt;asp:ListItem&gt;Dropdown&lt;/asp:ListItem&gt;
        &lt;/asp:DropDownList&gt;
    &lt;/div&gt;
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>In.cs code</p>

<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Dynamic_controls
{
    public partial class Dynamiccontrol : System.Web.UI.Page
    {
        Panel pnlTextBox;
        protected void Page_PreInit(object sender, EventArgs e)
        {
            //Create a Dynamic Panel
            pnlTextBox = new Panel();
            pnlTextBox.ID = "pnlTextBox";
            pnlTextBox.BorderWidth = 1;
            pnlTextBox.Width = 800;
            pnlTextBox.Height = 800;
            this.form1.Controls.Add(pnlTextBox);

            RecreateTxtControls("txtDynamic", "TextBox");
            RecreateDDLControls("ddlDynamic", "DropDownList");
            RecreateChkControls("chkDynamic", "CheckBox");

        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private int FindOccurence(string substr)
        {
            string reqstr = Request.Form.ToString();
            return ((reqstr.Length - reqstr.Replace(substr, "").Length) / substr.Length);
        }

    private int FindOccurenceCheckbox(string substr)
    {
        string reqstr = Request.Form.ToString();
        substr = "chkDynamic";
        return ((reqstr.Length - reqstr.Replace(substr, "").Length) / substr.Length);
    }

    private int FindOccurenceLabel(string substr)
    {
        string reqstr = Request.Form.ToString();
        substr = "lblDynamic";
        return ((reqstr.Length - reqstr.Replace(substr, "").Length) / substr.Length);
    }


    private void RecreateTxtControls(string ctrlPrefix, string ctrlType)
    {
        string[] ctrls = Request.Form.ToString().Split('&amp;');
        int cnt = FindOccurence(ctrlPrefix);
        if (cnt &gt; 0)
        {
            for (int k = 1; k &lt;= cnt; k++)
            {
                for (int i = 0; i &lt; ctrls.Length; i++)
                {
                    if (ctrls[i].Contains(ctrlPrefix + "-" + k.ToString()) &amp;&amp; !ctrls[i].Contains("EVENTTARGET"))
                    {
                        string ctrlID = ctrls[i].Split('=')[0];

                        if (ctrlType == "TextBox")
                        {
                            CreateTextBox(ctrlID);
                        }

                        break;
                    }

                }

            }
        }
    }

    private void RecreateDDLControls(string ctrlPrefix, string ctrlType)
    {
        string[] ctrls = Request.Form.ToString().Split('&amp;');
        int cnt = FindOccurence(ctrlPrefix);
        if (cnt &gt; 0)
        {
            for (int k = 1; k &lt;= cnt; k++)
            {
                for (int i = 0; i &lt; ctrls.Length; i++)
                {
                    if (ctrls[i].Contains(ctrlPrefix + "-" + k.ToString()) &amp;&amp; !ctrls[i].Contains("EVENTTARGET"))
                    {
                        string ctrlID = ctrls[i].Split('=')[0];

                        if (ctrlType == "DropDownList")
                        {
                            CreateDropDownList(ctrlID);
                        }
                        break;
                    }
                }
            }
        }
    }

    private void RecreateChkControls(string ctrlPrefix, string ctrlType)
    {
        string[] ctrls = Request.Form.ToString().Split('&amp;');
        // ctrlPrefix = "chkDynamic";
        int cnt = FindOccurenceCheckbox(ctrlPrefix);
        if (cnt &gt; 0)
        {
            for (int k = 1; k &lt;= cnt; k++)
            {
                for (int i = 0; i &lt; ctrls.Length; i++)
                {
                    if (ctrls[i].Contains(ctrlPrefix + "-" + k.ToString()) &amp;&amp; !ctrls[i].Contains("EVENTTARGET"))
                    {
                        string ctrlID = ctrls[i].Split('=')[0];

                        if (ctrlType == "CheckBox")
                        {
                            CreateCheckbox(ctrlID);
                        }
                        break;
                    }
                }
            }
        }
    }

    private void ReqFieldValidator(string ctrlPrefix, string ctrlType)
    {
        string[] ctrls = Request.Form.ToString().Split('&amp;');
        // ctrlPrefix = "chkDynamic";
        int cnt = FindOccurenceCheckbox(ctrlPrefix);
        if (cnt &gt; 0)
        {
            for (int k = 1; k &lt;= cnt; k++)
            {
                for (int i = 0; i &lt; ctrls.Length; i++)
                {
                    if (ctrls[i].Contains(ctrlPrefix + "-" + k.ToString()) &amp;&amp; !ctrls[i].Contains("EVENTTARGET"))
                    {
                        string ctrlID = ctrls[i].Split('=')[0];

                        if (ctrlType == "RequiredFieldValidator")
                        {
                            CreateCheckbox(ctrlID);
                        }
                        break;
                    }
                }
            }
        }
    }

    private void ReqLabel(string ctrlPrefix, string ctrlType)
    {
        string[] ctrls = Request.Form.ToString().Split('&amp;');
        int cnt = FindOccurenceCheckbox(ctrlPrefix);
        if (cnt &gt; 0)
        {
            for (int k = 1; k &lt;= cnt; k++)
            {
                for (int i = 0; i &lt; ctrls.Length; i++)
                {
                    if (ctrls[i].Contains(ctrlPrefix + "-" + k.ToString()) &amp;&amp; !ctrls[i].Contains("EVENTTARGET"))
                    {
                        string ctrlID = ctrls[i].Split('=')[0];

                        if (ctrlType == "Label")
                        {
                            CreateLabel(ctrlID);
                        }
                        break;
                    }
                }
            }
        }
    }



    private void CreateLabel(string ID)
    {
        Label lbl = new Label();
        lbl.ID = ID;
        lbl.Text = "text";
        pnlTextBox.Controls.Add(lbl);
        Literal lt = new Literal();
        lt.Text = "&lt;br /&gt;";
        pnlTextBox.Controls.Add(lt);
    }
    private void CreateTextBox(string ID)
    {
        //CreateLabel(ID);
        TextBox txt = new TextBox();
        txt.ID = ID;
        pnlTextBox.Controls.Add(txt);
        // txt.AutoPostBack = true;
        // txt.TextChanged += new EventHandler(OnTextChanged);
        RequiredFieldValidator req = new RequiredFieldValidator();
        req.ControlToValidate = txt.ID;
        req.EnableViewState = false;

        pnlTextBox.Controls.Add(txt);
        Literal lt = new Literal();
        lt.Text = "&lt;br /&gt;";

    }


    private void CreateCheckbox(string ID)
    {
        CheckBox chk = new CheckBox();
        chk.ID = ID;
        chk.Checked = true;
        chk.CheckedChanged += new EventHandler(chk_CheckedChanged);
        chk.AutoPostBack = true;
        chk.EnableViewState = false;
        pnlTextBox.Controls.Add(chk);
        Literal lt = new Literal();
        lt.Text = "&lt;br /&gt;";
        pnlTextBox.Controls.Add(lt);
    }

    void chk_CheckedChanged(object sender, EventArgs e)
    {
        try
        {

        }
        catch (Exception ex)
        {

        }
    }

    private void CreateDropDownList(string ID)
    {
        DropDownList ddl = new DropDownList();
        ddl.ID = ID;
        ddl.Items.Add(new ListItem("--Select--", ""));
        ddl.Items.Add(new ListItem("One", "1"));
        ddl.Items.Add(new ListItem("Two", "2"));
        ddl.Items.Add(new ListItem("Three", "3"));
        // ddl.AutoPostBack = true;
        ddl.EnableViewState = false;
        // ddl.SelectedIndexChanged += new EventHandler(OnSelectedIndexChanged);
        pnlTextBox.Controls.Add(ddl);

        //int cnt2 = FindOccurence("CheckBox");
        //CreateCheckbox("CheckBox-" + Convert.ToString(cnt2 + 1));

        Literal lt = new Literal();
        lt.Text = "&lt;br /&gt;";
        pnlTextBox.Controls.Add(lt);
    }

    private void CreateRequiredFieldValidator(string ID)
    {
        RequiredFieldValidator req = new RequiredFieldValidator();
        req.ID = ID;
        // ddl.AutoPostBack = true;
        // ddl.SelectedIndexChanged += new EventHandler(OnSelectedIndexChanged);
        pnlTextBox.Controls.Add(req);
        Literal lt = new Literal();
        lt.Text = "&lt;br /&gt;";
        pnlTextBox.Controls.Add(lt);
    }

    protected void OnTextChanged(object sender, EventArgs e)
    {
        TextBox txt = (TextBox)sender;
        string ID = txt.ID;
        ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", "&lt;script type = 'text/javascript'&gt;alert('" + ID + " fired OnTextChanged event');&lt;/script&gt;");
        //Place the functionality here
    }

    protected void ddlDynamic_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlcontrols.SelectedValue == "Textbox")
        {
            int cnt = FindOccurence("txtDynamic");
            CreateTextBox("txtDynamic-" + Convert.ToString(cnt + 1));

            int cntChk = FindOccurenceCheckbox("CheckBoxdll");
            CreateCheckbox("chkDynamic-" + Convert.ToString(cntChk + 1));
        }
        if (ddlcontrols.SelectedValue == "Dropdown")
        {
            int cnt = FindOccurence("ddlDynamic");
            CreateDropDownList("ddlDynamic-" + Convert.ToString(cnt + 1));

            int cntChk = FindOccurenceCheckbox("chkDynamic");
            CreateCheckbox("chkDynamic-" + Convert.ToString(cntChk + 1));


        }
    }
}

}
</code></pre>
