Get value of dynamic textbox when button click

                              Get value of  dynamic textbox  when button click


<html>
<head id="Head1" runat="server">
    <title>sample</title>

    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                var lstTextBox = new List<TextBox>();

                for (int i = 0; i <= 4; i++)
                {
                    TextBox txtbx = new TextBox();
                    txtbx.ID = string.Format("txtbx{0}", i);
                    // txtbx.AutoPostBack = true;
                    lstTextBox.Add(txtbx);
                    txtbx.Text = "initial value" + i.ToString();
                    Panel1.Controls.Add(txtbx);
                }
                Session["lstTextBox"] = lstTextBox;

            }
            else
            {
                List<TextBox> lstTextBox = (Session["lstTextBox"] as List<TextBox>);
                foreach (TextBox tb in lstTextBox)
                {
                    Panel1.Controls.Add(tb);
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            List<TextBox> lstTextBox = (Session["lstTextBox"] as List<TextBox>);

            string lblResult = "";
            var p = form1.FindControl("Panel1");
            foreach (var item in lstTextBox)
            {
                TextBox tb = item as TextBox;
                var theTextBox = (TextBox)Panel1.FindControl(tb.ID);
                lblResult += theTextBox.Text;
            }

            lbltest.Text= lblResult;
        }
    
    </script>

</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Panel runat="server" ID="Panel1" />
            <br />
            <asp:Button runat="server" ID="Button1" OnClick="Button1_Click" Text="click" />
            <br />
            <asp:Label runat="server" ID="lbltest" Text="default text" />

        </div>
    </form>
</body>
</html>

Comments

Popular posts from this blog

Bootstrap Modal Popup keep open on PostBack in ASP.Net

Resolved Issue in Asp core 3.0 serializersettings does not exist in AddJsonOptions

Dynamic Mega Menu With Asp.Net with c#,java script and Sql Server.