Home > ASP.Net, Javascript > Create Dynamic Textbox

Create Dynamic Textbox


Hi

try this example to Create Dynamic Textbox

<script runat="server" language="C#">

    int count = 1;
   
    void IterateThroughChildren(Control parent)
    {
      foreach (Control c in parent.Controls)
      {
        if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox") &&
              c.ID == null)
        {
          ((TextBox) c).Text = "TextBox " + count.ToString();
          ((TextBox) c).Columns = 10;
          count++;
        }
       
        if (c.Controls.Count > 0)
        {         
          IterateThroughChildren(c);         
        }
      }
    }

    void CreateTextBoxes(Object sender, EventArgs e)
    {
      if (!Page.IsValid) return;
     
      int n = Int32.Parse(txtTBCount.Text);
     
      // now, create n TextBoxes, adding them to the PlaceHolder TextBoxesHere
      for (int i = 0; i < n; i++)
      {
        TextBoxesHere.Controls.Add(new TextBox());
      }
     
      // now, set the Text property of each TextBox
      IterateThroughChildren(this);
    }
</script>

<form runat="server">
    How many TextBoxes would you like to create? (<i>Please choose vaule between 1 and 10</i>)<br />
    <asp:textbox runat="Server" id="txtTBCount" Columns="3" />
    <asp:RangeValidator runat="server" ControlToValidate="txtTBCount"
             MinimumValue="1" MaximumValue="10" Type="Integer"
             ErrorMessage="Make sure that you choose a value between 1 and 10!" />
    <br />
    <asp:button runat="server" Text="Create Dynamic TextBoxes"
           OnClick="CreateTextBoxes" />
 <p>
 <asp: PlaceHolder runat="server" id="TextBoxesHere" />
</form>

Good Luck

Advertisements
Categories: ASP.Net, Javascript
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: