In Solution Explorer, click Show All Files. The list of files that are associated with WebForm1.aspx
appears. Open WebForm1.aspx.vb.
Declare the TestBox controls in the .vb (code-behind) file. Also, declare a variable
for the existing form element in the .aspx file. Update the declarations that
follow the declaration for the WebForm1 public class:
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Label4 As System.Web.UI.WebControls.Label
Protected WithEvents Label3 As System.Web.UI.WebControls.Label
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
' Added by hand for access to the form.
Protected Form1 As System.Web.UI.HtmlControls.HtmlForm
' Added by hand; will create instance in OnInit.
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents TextBox2 As System.Web.UI.WebControls.TextBox
The TextBox declarations are entered by hand as they would be if a TextBox were dragged from the toolbox to the ASPX page. However, in this
case, you create the controls dynamically.
Add code to create the TextBox controls dynamically. The controls are created every time that
the page is run. The best place to do this is in the Page_Init function that the WebForm1 class provides. Locate the Page_Init function. Expand the code that is marked with the comment "Web
Form Designer generated code." Modify the Page_Init functions, so that they appear similar to the following:
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
' Create dynamic controls here.
TextBox1 = New TextBox()
TextBox1.ID = "TextBox1"
TextBox1.Style("Position") = "Absolute"
TextBox1.Style("Top") = "25px"
TextBox1.Style("Left") = "100px"
Form1.Controls.Add(TextBox1)
TextBox2 = New TextBox()
TextBox2.ID = "TextBox2"
TextBox2.Style("Position") = "Absolute"
TextBox2.Style("Top") = "60px"
TextBox2.Style("Left") = "100px"
Form1.Controls.Add(TextBox2)
' CODEGEN: The Web Form Designer requires this method call.
' Do not modify it by using the code editor.
InitializeComponent()
End Sub
This code dynamically creates two TextBox controls, sets their IDs and positions, and then binds them to
the Form Controls collection. You can also add Web Forms Panel controls to the ASPX page, and then bind the text boxes to those
controls in the Page_Init function, as in the following example:
TextBox1 = New TextBox()
TextBox1.ID = "TextBox1"
' comment add command the Form Controls collection as follows
' Form1.Controls.Add(TextBox1)'
Panel1.Controls.Add(TextBox1)
Note When you create dynamic controls on a Web Form, you must create
the controls and add them to the controls collection in either the Page_Init event handler or the Page_Load event handler. Otherwise, the controls may not behave as
expected.
Initialize the Text property for the text boxes. Modify the existing Page_Load function:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
' Set the initial properties for the text boxes.
TextBox1.Text = "TextBox1"
TextBox2.Text = "TextBox2"
End If
End Sub
You must set the initial value (If Not IsPostBack) of the text boxes only one time. The IPostBackDataHandler interface for the text boxes maintains this information. You do
not have to reset the value for later posts.
Provide a handler for the TextBox TextChanged events. Add the following code after the Page_Load function:
Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
Dim txtBoxSender As TextBox
Dim strTextBoxID As String
txtBoxSender = CType(sender, TextBox)
strTextBoxID = txtBoxSender.ID
Select Case strTextBoxID
Case "TextBox1"
Label3.Text = "TextBox1 text was changed"
Case "TextBox2"
Label4.Text = "TextBox2 text was changed"
End Select
End Sub
This code verifies which control triggered the event, and then reports
this to the user by using the appropriate Label control. Notice that this function handles the TextChanged event for both of the dynamically created TextBox controls. By default, AutoPostBack is false for the TextBox controls. Therefore, if a user changes the text in the controls,
this action does not cause a PostBack to the server. However, when the user clicks
Submit to post the form to the server, this action triggers
the TextChanged events for the TextBox controls, and then this function is called.