Implementing ASP.NET Server Side Controls through Code

There are scenarios that you might face where you cannot find out the actual number of server controls that needs to be added to the web page.

Under such circumstances you might need to add the server controls dynamically. The number of controls that are added is determined by some other factor or value that you get when the application is run.

You can create controls dynamically and add it to the web page when required. For this purpose you need to place a placeholder in the page during the design time.

The placeholder is the place where the server controls will be added during runtime. In the code you will determine which type of control is going to be added to the placeholder.

For example if you are going to add a textbox control with value “This is dynamically added”, to the placeholder placeholderTB, then the code would look like,

Dim Author As New TextBox
Author.Text = “This is dynamically added”
placeholderTB.Controls.Add(Author)

You can also add events to these server controls that you created dynamically.