Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6885837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:41:05+00:00 2026-05-27T05:41:05+00:00

I am creating an dynamic controls I have an drop down with options Textbox,

  • 0

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

Let me say I create control like these from selecting the option from the dropdown.

1: text box 
2: drop down 
3: text box:
4: dropdown

However in the page output its shown like this:

1: textbox
2: textbox
3: dropdown
4: dropdown

This is not the right order based on the selected order above.

Issue here is the design is not quite right.

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

Pls find attched code any suggestion how to solve this would be helpful.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Dynamiccontrol.aspx.cs" Inherits="Dynamic_controls.Dynamiccontrol" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

In.cs 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('&');
        int cnt = FindOccurence(ctrlPrefix);
        if (cnt > 0)
        {
            for (int k = 1; k <= cnt; k++)
            {
                for (int i = 0; i < ctrls.Length; i++)
                {
                    if (ctrls[i].Contains(ctrlPrefix + "-" + k.ToString()) && !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('&');
        int cnt = FindOccurence(ctrlPrefix);
        if (cnt > 0)
        {
            for (int k = 1; k <= cnt; k++)
            {
                for (int i = 0; i < ctrls.Length; i++)
                {
                    if (ctrls[i].Contains(ctrlPrefix + "-" + k.ToString()) && !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('&');
        // ctrlPrefix = "chkDynamic";
        int cnt = FindOccurenceCheckbox(ctrlPrefix);
        if (cnt > 0)
        {
            for (int k = 1; k <= cnt; k++)
            {
                for (int i = 0; i < ctrls.Length; i++)
                {
                    if (ctrls[i].Contains(ctrlPrefix + "-" + k.ToString()) && !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('&');
        // ctrlPrefix = "chkDynamic";
        int cnt = FindOccurenceCheckbox(ctrlPrefix);
        if (cnt > 0)
        {
            for (int k = 1; k <= cnt; k++)
            {
                for (int i = 0; i < ctrls.Length; i++)
                {
                    if (ctrls[i].Contains(ctrlPrefix + "-" + k.ToString()) && !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('&');
        int cnt = FindOccurenceCheckbox(ctrlPrefix);
        if (cnt > 0)
        {
            for (int k = 1; k <= cnt; k++)
            {
                for (int i = 0; i < ctrls.Length; i++)
                {
                    if (ctrls[i].Contains(ctrlPrefix + "-" + k.ToString()) && !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 = "<br />";
        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 = "<br />";

    }


    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 = "<br />";
        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 = "<br />";
        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 = "<br />";
        pnlTextBox.Controls.Add(lt);
    }

    protected void OnTextChanged(object sender, EventArgs e)
    {
        TextBox txt = (TextBox)sender;
        string ID = txt.ID;
        ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", "<script type = 'text/javascript'>alert('" + ID + " fired OnTextChanged event');</script>");
        //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));


        }
    }
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:41:06+00:00Added an answer on May 27, 2026 at 5:41 am

    I’m not sure what your question is. Your code is clearly creating all controls of one type at once, so that is what I would expect to see in your page output.

    If you want to interleave controls then you need to change your algorithm to process controls one by one instead of by type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating huge amounts of dynamic controls, and for everything that must have
Was wondering if anyone had any smart approaches to creating dynamic vanity user urls
I have a Repeater control that is creating a dynamic amount of CheckBoxList controls
i am creating dynamic controls in my asp.net application. Each time i have to
in my application i creating a dynamic controls like ( 2 label controls in
I am creating dynamic controls using jQuery by reading the query string parameter. These
What choices do I have for creating stateful dynamic content in an ASP.Net web
ref: Dynamic Control ID Does anyone have a working example of creating the ID
I'm creating a dynamic dropdown list on my page. When an option is selected
When creating dynamic ajax controls you may experience a pre-render issue on postbacks. You

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.