Microsoft KB Archive/317515

= HOW TO: Dynamically Create Controls in ASP.NET with Visual Basic .NET =

Article ID: 317515

Article Last Modified on 2/11/2004

-

APPLIES TO


 * Microsoft ASP.NET 1.0
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft ASP.NET 1.1
 * Microsoft Visual Basic .NET 2003 Standard Edition

-



This article was previously published under Q317515



For a Microsoft Visual C# .NET version of this article, see 317794.

IN THIS TASK

 * SUMMARY
 * Create the Project and the Static Control
 * Create the Dynamic Control and Hook it Up
 * Save, Build, and Then Run the Sample



SUMMARY
This step-by-step article describes how to dynamically create controls for an ASPX Web page.

The sample project does the following:
 * It creates two TextBox controls.
 * It verifies that the TextBox contents (TextBox.text) and attributes are saved across posts to the server.
 * It describes how events that are posted by a dynamically created control are handled.

back to the top

Create the Project and the Static Control
 In Visual Studio .NET, create a new Web project by using Visual Basic .NET. Name the project DynamicCreate .  Open the WebForm1.aspx file, and then switch to HTML view. Insert the following code between the  tag and the  tag:  WebForm1       <form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;> <asp:Button id=&quot;Button1&quot; style=&quot;Z-INDEX: 100; LEFT: 23px; POSITION: absolute; TOP: 108px&quot; runat=&quot;server&quot; Text=&quot;Submit&quot; Height=&quot;27px&quot; Width=&quot;100px&quot;></asp:Button> <asp:Label id=&quot;Label4&quot; style=&quot;Z-INDEX: 105; LEFT: 23px; POSITION: absolute; TOP: 197px&quot; runat=&quot;server&quot; Width=&quot;368px&quot; EnableViewState=&quot;False&quot;></asp:Label> <asp:Label id=&quot;Label3&quot; style=&quot;Z-INDEX: 104; LEFT: 23px; POSITION: absolute; TOP: 163px&quot; runat=&quot;server&quot; Width=&quot;368px&quot; EnableViewState=&quot;False&quot;></asp:Label> <asp:Label id=&quot;Label2&quot; style=&quot;Z-INDEX: 102; LEFT: 23px; POSITION: absolute; TOP: 60px&quot; runat=&quot;server&quot; Width=&quot;86px&quot; Height=&quot;19px&quot;> TextBox2:</asp:Label> <asp:Label id=&quot;Label1&quot; style=&quot;Z-INDEX: 101; LEFT: 23px; POSITION: absolute; TOP: 28px&quot; runat=&quot;server&quot; Width=&quot;86&quot; Height=&quot;19&quot;> TextBox1:</asp:Label> </li> Switch back to Design view to see the statically created controls that the project will use.</li></ol>

back to the top

Create the Dynamic Control and Hook it Up
<ol> In Solution Explorer, click Show All Files. The list of files that are associated with WebForm1.aspx appears. Open WebForm1.aspx.vb.</li>  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. </li>  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 &quot;Web Form Designer generated code.&quot; 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 = &quot;TextBox1&quot; TextBox1.Style(&quot;Position&quot;) = &quot;Absolute&quot; TextBox1.Style(&quot;Top&quot;) = &quot;25px&quot; TextBox1.Style(&quot;Left&quot;) = &quot;100px&quot; Form1.Controls.Add(TextBox1)

TextBox2 = New TextBox TextBox2.ID = &quot;TextBox2&quot; TextBox2.Style(&quot;Position&quot;) = &quot;Absolute&quot; TextBox2.Style(&quot;Top&quot;) = &quot;60px&quot; TextBox2.Style(&quot;Left&quot;) = &quot;100px&quot; 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 = &quot;TextBox1&quot; ' 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. </li>  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 = &quot;TextBox1&quot; TextBox2.Text = &quot;TextBox2&quot; 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. </li>  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 &quot;TextBox1&quot; Label3.Text = &quot;TextBox1 text was changed&quot;

Case &quot;TextBox2&quot; Label4.Text = &quot;TextBox2 text was changed&quot; 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. </li></ol>

back to the top

Save, Build, and Then Run the Sample
Save, and then build the sample. To run the sample in Visual Studio .NET, right-click the ASPX file, and then click View in Browser.

back to the top

Keywords: kbhowtomaster kbwebforms kbctrlcreate kbservercontrols kbevent KB317515

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.