Search - Articles - Dev Forums - Favorites - Member Login
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009


Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Build the right apps the right way with powerful development tools.
Visual Studio 2010. Learn more...
Creating a DropDownList control in code

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:

 

Public Sub btnOk_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOk.Click

        Dim i As Integer

        Dim j As Integer

        For i = 1 To txtNumber.Text

            Dim myDDL = New DropDownList

            myDDL.id = "ddlDynamic" & i

            For j = 1 To 3

                Dim myLI As New ListItem

                myLI.Text = "Control Number: " & i & "-" & j

                myLI.Value = i & j

                myDDL.Items.add(myLI)

            Next

            Me.ph1.Controls.Add(myDDL)

            Dim myLiteral = New LiteralControl

            myLiteral.Text = "<BR><BR>"

            Me.ph1.Controls.Add(myLiteral)

        Next

    End Sub  

 

For this page, two variables are needed for loops:

 

        Dim i As Integer

        Dim j As Integer

 

The other loop iterates through the number of DropDownList controls to add:

 

        For i = 1 To txtNumber.Text

 

You then create a DropDownList control in code:

 

           Dim myDDL = New DropDownList

 

And give that control a name:

 

           myDDL.id = "ddlDynamic" & i

 

Then the inner loop is defined. This is where the ListItem controls will be added to the DropDownList controls:

 

            For j = 1 To 3

 

A ListItem control is defined:

 

                Dim myLI As New ListItem

 

And is given a value for its’ Text and Value properties:

 

  myLI.Text = "Control Number: " & i & "-" & j

  myLI.Value = i & j

 

The ListItem is added to the Items collection of the DropDownList control through the Add method:

 

                myDDL.Items.add(myLI)

 

After the inner loop adds all the ListItem controls, the DropDownList control is added to the form on the page:

 

            Me.ph1.Controls.Add(myDDL)

 

Next, a LiteralControl is defined. This control provides a way for us to place some formatting HTML text between each DropDownList control:

 

         Dim myLiteral = New LiteralControl

 

The formatting text is simply two line break tags:

 

            myLiteral.Text = "<BR><BR>"

 

The Literal Control is added to the Controls collection of the Form:

 

            Me.ph1.Controls.Add(myLiteral)

 

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.

Article Comments
I tried with this way to create DDL dynamically. Very smoothly it creates. But when I'm trying to get the selected values from those DDL then I could not found those control handle. FindControl() function can not find those control.

What is the solution.

Thanks

Posted on 11/2/2008 4:07:32 AM by Shohel

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Creating a basic Repeater control

Disclaimer - Privacy
© 2002-2010 DevASP.net