How to: Add Controls to an ASP.NET Web Page Programmatically
At times it is more practical to create a control at run time than at design time. For example, imagine a search results page in which you want to display results in a table. Because you do not know how many items will be returned, you want to dynamically generate one table row for each returned item.
Existing controls can often provide the functionality you get from creating controls dynamically. For example, controls such as the, , and controls can dynamically create rows or other control elements when the page runs.
In order to programmatically add a control to a page, there must be a container for the new control. For example, if you are creating table rows, the container is the table. If there is no obvious control to act as container, you can use aor Web server control.
In some instances, you might want to create both static text and controls. To create static text, you can use either a Dynamic Web Server Controls and View State.or a Web server control. You can then add these controls to the container as you would any other control. For information about view state in controls created at run time, see
To add a control to an ASP.NET Web page programmatically
Create an instance of the control and set its properties, as shown in the following example:
Controls are typically added to the page during the page's initialization stage. For details about page stages, see.
Add the new control to the Controls collection of a container already on the page, as shown in the following example:
Because the Controls property is a collection, you can use the Dynamic Web Server Controls and View State.method to place the new control at a specific location — for example, in front of other controls. However, this can introduce errors into the page. For details, see
The following code example shows the event handler for theevent of a control named DropDownList1. The handler creates as many label controls as the user has selected from the drop-down list. The container for the controls is a PlaceHolder Web server control named Placeholder1.
User input in a Web page can include potentially malicious client script. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information,.
Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged Dim DropDownList1 As New DropDownList() Dim PlaceHolder1 As New PlaceHolder() Dim i As Integer Dim numlabels As Integer ' Get the number of labels to create. numlabels = CInt(DropDownList1.SelectedItem.Text) For i = 1 To numlabels Dim myLabel As Label = New Label() ' Set the label's Text and ID properties. myLabel.Text = "Label " & i myLabel.ID = "Label" & i PlaceHolder1.Controls.Add(myLabel) ' Add a spacer in the form of an HTML <br /> element Dim spacer As LiteralControl = New LiteralControl("<br />") PlaceHolder1.Controls.Add(spacer) Next End Sub