ASP.NET Web Forms Web Forms Server Controls Programming Web Forms Server Controls
In certain cases, a developer may need to create a control ( or children of a container control ) at run time rather than at design time. Typical scenarios include dynamically adding choices to an <HtmlSelect
> or <asp:DropDownList
> control to display user options obtained either thru user input or by looping thru a given enumeration.
Dynamically generated controls are usually added to a container control that supports a collection interface, such as the Items collection of list boxes and grids, the Columns collection of data tables, the generic Controls collection of most container controls including the page itself, and so on.
This means that in order to programmatically add a control to a page, there must be a container for the new control, whether defined at design time or at run time. For instance, as shown in the sample, there must be an existing HtmlSelect or DropDownList control if you are creating a list of options.
NOTE: While you can dynamically add controls directly to the page itself, the layout of the controls are a lot easier to manipulate when the controls are within containers.
In some instances, you may need to create static text aside from the controls. To create static text, you can use either the LiteralControl object or a Label Web server control. If there is no obvious control to act as a container, you can use a Placeholder or Panel Web server control. You can then add the text controls to the containers just as you would any other control.
- Create an instance of the control and set its properties:
Label myLabel = new Label ( );
myLabel.Text = "Sample Label"
Dim myLabel as New Label ( )
myLabel.Text = "Sample Label" |
|
C# |
VB |
- Add the new control to the Controls collection of a container declared on the page:
myPanel.Controls.Add ( myLabel );
myPanel.Controls.Add ( myLabel ) |
|
C# |
VB |
Below are links to further code examples that illustrate adding controls to a Web Forms page in practical ASP.NET applications.
When a control is created dynamically at run time, some information about the control is stored in the view state that is rendered with the page. When the page is posted back to the server, however, non-dynamic controls ( those defined on the page ) are instantiated in the Page.Init event and view state information is loaded before the dynamic controls can be recreated ( usually in the Page_Load handler ).
Effectively, before the dynamic controls are recreated, view state is temporarily out of sync with the page’s controls. After the Page_Load event has run, but before control event handling methods are called, the remaining view state information is loaded into the dynamically created controls.
In most scenarios, this view state processing model works fine. Typically, you add dynamic controls to the end of a container’s collection of controls. The view state information stored for the dynamic controls is therefore extra information at the end of the view state structure for the appropriate container, and the page can ignore it until after the controls are created.
However, view state information about dynamically created controls can be a problem in two scenarios:
- If you insert dynamic controls between existing controls.
- If you insert controls dynamically and then reinsert them during a round trip, but with different values.
If you insert dynamic controls between existing controls, the dynamic control’s view state information is inserted into the corresponding location of the view state structure. When the page is posted and the view state is loaded, the dynamic control does not yet exist; therefore, the extra information in view state does not correspond to the right control. The result is usually an error indicating an invalid cast.
If you reinsert controls with each round trip, each generation of dynamically created controls will pick up property values from the view state of the preceding set of controls. In many cases, you can avoid this problem by setting the EnableViewState property of the container control to false. In that case, no information about the dynamic controls is saved, and there is no conflict with successive versions of the controls.
For details about view state, see Web Forms Page Processing Stages and Introduction to Web Forms State Management.
Setting Web Server Control Properties Programmatically Setting HTML Server Control Properties Programmatically