Author: Faraz
This article will explains you, the technique of creating a DropDownList control at Run time. In this technique the magic man is Place Holder control. Read on to learn more!
In addition to manually defining DropDownList controls on you page, you can also dynamically add those controls through code. You would typically do this in a situation where you didn’t know ahead of time how many controls you needed.
For example, you may have a job application page where visitors must answer a series of multiple choice questions such as the number of years of education, whether they have specific skills, or how long they have worked in a profession. You may want such page to dynamic enough so that a manager could add a new question to the database with its possible answers and you wouldn’t have to change the definition of the ASP.Net page. Therefore, you would need to dynamically add an unknown number of DropDownList controls to you page.
The technique presented here prompts visitors for a number. Then, based on that number, DropDownList controls are added to the page. And added to each DropDownList control are a series of ListItem controls.
To start with this project, select the New Project option from the Start Up page. Add the four controls. The first one is the Label control set its text (“Enter the Number of DropDownList controls you want to display :”). Now add the Text box control and the Button control, set their IDs (“txtNumber” and “btnOk” respectively). Select the OnClick event (“btnOk_Click”) for the button control. Finally, add the Place holder control, which plays a vital role in this technique. Set its ID (“ph1”).
The text box control retrieves from visitors the number of DropDownList controls to be added. The button control is used to submit the page for processing. Notice that the form is given a name. That is required in this situation so we can add controls to form.
When the OK button is clicked, the following code dynamically adds the DropDownList controls:
For this page, two variables are needed for loops:
The other loop iterates through the number of DropDownList controls to add:
You then create a DropDownList control in code:
And give that control a name:
Then the inner loop is defined. This is where the ListItem controls will be added to the DropDownList controls:
A ListItem control is defined:
And is given a value for its’ Text and Value properties:
The ListItem is added to the Items collection of the DropDownList control through the Add method:
After the inner loop adds all the ListItem controls, the DropDownList control is added to the form on the page:
Next, a LiteralControl is defined. This control provides a way for us to place some formatting HTML text between each DropDownList control:
The formatting text is simply two line break tags:
The Literal Control is added to the Controls collection of the Form:
The code then iterates back to the top of the procedure. Notice the dynamically generated DropDownList controls. Also notice that the ListItem controls displayed in the DropDownList controls all contain a unique value as was created in the inner loop of the code.