VB.NET - Adding TextBox dynamically on every button click

Asked By kalyan on 31-May-11 01:48 AM
How to add a TextBox dynamically at run time on every click of a button?
Jitendra Faye replied to kalyan on 31-May-11 01:57 AM

Add the following code fragment to the Click event of our button:




Protected Sub Button1_Click( ByVal sender As Object , ByVal e As System.EventArgs) _
  Handles Button1.Click  
    Dim t As TextBox
  t = New TextBox
  Panel1.Controls.Add(t)
End Sub

Using this you can add TextBox To Panel Control.

Riley K replied to kalyan on 31-May-11 01:58 AM

 Try below code  in button click

          TextBox txt = new TextBox();
          txt.ID = "TextBox1";
          Page.Form.Controls.Add(txt);

Venkat K replied to kalyan on 31-May-11 02:03 AM
You can use the below function, it will wait the user to input the number of textboxes to create dynamically. You can change the name of the textbox as per your need.

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
      Dim I As Integer
      For I = 1 To txtNumber.Text
        Dim myTextBox = New TextBox
        myTextBox.ID = "txtDynamic" & I
        myTextBox.Text = "Control Number:" & I
        Me.ph1.Controls.Add(myTextBox)
        Dim myLiteral = New LiteralControl
        myLiteral.Text = "<BR><BR>"
        Me.ph1.Controls.Add(myLiteral)
      Next
    End Sub

Thanks
Reena Jain replied to kalyan on 31-May-11 02:05 AM
hi,

try following code to create the TextBox and Label controls dynamically. The controls are created every time that the page is run. The best place to do this is in the OnInit function that the WebForm1 class provides.

Locate the OnInit function. Expand the code that is marked with the "Web Form Designer generated code" comment. Modify the OnInit function so that it looks similar to the following code:

Protected Overrides Sub OnInit(e As EventArgs)
  ' Create dynamic controls here.
  ' Use "using System.Web.UI.WebControls;"
  TextBox1 = New TextBox()
  TextBox1.ID = "TextBox1"
  TextBox1.Style("Position") = "Absolute"
  Form1.Controls.Add(TextBox1)
 
  Label1 = New Label()
  Label1.ID = "Label1"
  TextBox2.Style("Position") = "Absolute"
  Form1.Controls.Add(Label1)
 
  Me.TextBox1.TextChanged += New System.EventHandler(Me.TextBox_TextChanged)
  '
  ' CODEGEN: This call is required by the ASP.NET Web Form Designer.
  '
  InitializeComponent()
  MyBase.OnInit(e)
End Sub

This code dynamically creates TextBox and Label controls, sets their IDs and positions, and then binds them to the Form Controls collection. The code also wires up the TextChanged events of the text box to a handler (TextBox_TextChanged).

Hope this will help you
Anoop S replied to kalyan on 31-May-11 02:02 PM
To begin with the dynamic controls do not have any object reference in the code behind class of page and have only in page control collection..
hence, you need to add them to the viewstate and load the values from there
here is a sample code on how to accomplish this..
http://www.codeproject.com/KB/viewstate/retainingstate.aspx