locked
How to create dynamic control in forms using asp.net web form RRS feed

  • Question

  • User-471420332 posted

    How to create dynamic control in forms using asp.net web form, I have to create a form build for my C# asp.net web form application, please share best example or code.

    Monday, February 4, 2019 11:40 AM

All replies

  • User839733648 posted

    Hi mazhar khan india,

    According to your description, I suggest that you could use PlaceHolder to add controls dynamically.

    I've made a sample on my side. And in my sample, I also add submiting the content in the textbox control by using ViewState.

    Here is my code. Hope it will be helpful to you.

    .Aspx

        <form id="form1" runat="server">
            <div>
                <asp:Button ID="AddControlButton" runat="server" Text="Add Control" OnClick="AddControlButton_Click"/>
                <br />
                <asp:PlaceHolder ID="DynamicControlsHolder" runat="server"></asp:PlaceHolder>
                <br />
                <br />
                <asp:Button ID="Submit" runat="server" Text="Submit Form" OnClick="Submit_Click" />
                <br />
                <br />
                Form Contents:
                <br />         
                <asp:Label ID="ContentMessage" runat="server"></asp:Label>
            </div>
        </form>

    Code behind.

            private List<string> ControlsList
            {
                get
                {
                    if (ViewState["controls"] == null)
                    {
                        ViewState["controls"] = new List<string>();
                    }
                    return (List<string>)ViewState["controls"];
                }
            }
    
            private int NextID
            {
                get
                {
                    return ControlsList.Count + 1;
                }
            }
    
            protected override void OnPreInit(EventArgs e)
            {
                base.OnPreInit(e);
            }
    
            protected override void LoadViewState(object savedState)
            {
                base.LoadViewState(savedState);
    
                foreach(string txtxID in ControlsList)
                {
                    TextBox txt = new TextBox();
                    txt.ID = txtxID;
                    DynamicControlsHolder.Controls.Add(txt);
                    DynamicControlsHolder.Controls.Add(new LiteralControl("<br />"));
                }
            }
    
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void AddControlButton_Click(object sender, EventArgs e)
            {
                TextBox txt = new TextBox();
                txt.ID = "TextBox" + NextID.ToString();
                DynamicControlsHolder.Controls.Add(txt);
                DynamicControlsHolder.Controls.Add(new LiteralControl("<br />"));
                ControlsList.Add(txt.ID);
            }
    
            protected void Submit_Click(object sender, EventArgs e)
            {
                StringBuilder sb = new StringBuilder();
                foreach (Control ctl in DynamicControlsHolder.Controls)
                {
                    if(ctl is TextBox)
                    {
                        TextBox txt = ctl as TextBox;
                        if (ctl != null)
                        {
                            sb.Append(txt.ID);
                            sb.Append(":");
                            sb.Append(txt.Text);
                            sb.Append("<br/>");
                        }
                    }
                }
                ContentMessage.Text = sb.ToString();
            }

    Result:

    Best Regards,

    Jenifer

    Tuesday, February 5, 2019 7:20 AM
  • User1120430333 posted

    https://www.geeksforgeeks.org/displaying-xml-using-xslt/

    You can make all the controls dynamically using XML to HTML transformation. 

    Tuesday, February 5, 2019 11:35 AM
  • User-471420332 posted

    I need to submit data to database.

    Tuesday, February 5, 2019 12:29 PM
  • Tuesday, February 5, 2019 1:49 PM