Monday 5 August 2013

How to create Dynamic Textbox on button Click Event In Asp.Net.

In this article ,I have explained how to create dynamic  textbox on button click event in asp.net using c#. Dynamic control is easy to handle in the program. In this article I have created no of textbox  as per requirement of user or data.

Design.aspx:-

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1"
            runat="server" Text="Button" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

Code Behind Page:-

protected void Button1_Click(object sender, EventArgs e)
    {
        int no = Convert.ToInt32(TextBox1.Text);
       
       for (int i = 0; i < no; i++)
        {
            string name = "TextBox"+i.ToString();
            TextBox text = new TextBox();
            text.ID = name;
            text.Width=100;
            text.Height = 25;
            form1.Controls.Add(text);
           
        }
    }


When you will on click button control it will create no buttons as values inserted in textbox.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.