<DropDownList>

The DropdownList tag renders as a drop-down list control at run-time.

Syntax

<DropDownList
    AccessKey="string"
    AppendDataBoundItems="True|False"
    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"
    BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge| Inset|Outset"
    BorderWidth="size"
    CssClass="string"
    DataField="string"
    DataSourceID="string"
    DataTextField="string"
    DataTextFormatString="string"
    DataType="string|int32|...."
    DataValueField="string"
    Font-Bold="True|False"
    Font-Italic="True|False"
    Font-Names="string"
    Font-Overline="True|False"
    Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium| Large|X-Large|XX-Large"
    Font-Strikeout="True|False"
    Font-Underline="True|False"
    ForeColor="color name|#dddddd"
    Height="size"
    ID="string"
    Nullable="True|False"
    ParameterName="string"
    Style="string"
    TabIndex="integer"
    TargetControlId="string"
    TargetDataSourceId="string"
    ToolTip="string"
    Visible="True|False"
    Width="size">


    <listitem value="string" selected="True|False">Item1</listitem>
    <listitem value="string">Item2</listitem>
    ...
</DropDownList>

Remarks

Creating Dependent Lists/Cascading Lists

New In Version 1.5: Two drop-down list controls can be bound together so that the selected value in the first control determines the available values in the second control. To do this, you'll need to set a few properties on the 1st control and bind the 2nd control to a <ControlDataSource>.

Example

For this example, we'll be using the DNN Lists table. This table contains numerous values - among them are a list of Countries and, for some of those countries, a list of regions. We will set up two drop-down list controls. The 1st control will load a list of countries from the Lists table. The 2nd control will be dynamically loaded with a list of regions (if any) for the selected country. Not all countries will have regions.

<AddForm>
  <SubmitCommand CommandText="INSERT INTO Contacts(ContactName,Country,Region) 
                              VALUES(@ContactName,@Country,@Region)" />
  <ControlDataSource id="dsCountries" 
    CommandText="SELECT Text, EntryID FROM Lists WHERE ListName='Country' ORDER BY Text"/>
  <ControlDataSource id="dsRegions" 
    CommandText="SELECT Text, Value FROM Lists WHERE ParentID=@ParentID">
    <Parameter name="ParentID" />
  </ControlDataSource>
  ...
  <Label for="ddlCountries" text="Country" />
  <DropdownList id="ddlCountries" DataField="Country" DataType="string" 
    DataTextField="Text" DataValueField="EntryID" DataSourceId="dsCountries" 
    TargetDataSourceId="dsRegions" ParameterName="ParentID" TargetControlId="ddlRegions" />

  <Label for="ddlRegions" text="Region" /> 
  <DropdownList id="ddlRegions" DataField="Region" DataType="string" 
    DataTextField="Text" DataValueField="Value" DataSourceId="dsRegions" />
  
  <AddButton text="Add" /> &nbsp;<CancelButton text="Cancel" />
  
</AddForm>

In the example above, there are two <ControlDataSource> tags. The first loads the list of countries from the DNN Lists table and is linked to the "ddlCountries" drop-down list. The second one will be used to lookup the list of regions in the selected country.

The DNN Lists table is setup so that each record can be the "parent" of one or more other records. If a record has a parent, its "ParentID" column will contain the EntryID of its parent. So, for our example, to get a list of regions in a given country, we look for all records that have a ParentID that matches the EntryID of our country. So, we've setup the ControlDataSource to accept a "ParentID" parameter.

Next, we need to setup the Country drop-down list to send its value to the Regions ControlDataSource. It is set to load its data from the "dsCountries" ControlDataSource using the DataSourceId, DataTextField, and DataValueField attributes. To enable the control to cause the Regions drop-down list to reload its data, we:

  1. Specify the TargetControlId. This is the control we want to be dependent on this control's value. For the example, that is "ddlRegions".
  2. Specify the TargetDataSourceId. This is the ID of the ControlDataSource used to fill the target control. In this example, it is "dsRegions".
  3. Specify the ParameterName. This is the name of the parameter the target ControlDataSource is expecting in order to retrieve its data. In the example, this is "ParentID".

So, when a country is selected, a parameter will be created with the name "ParentID". It's value will be set to the value (not the display text) of the currently selected country. That parameter will then be passed to the target ControlDataSource (dsRegions) and the target control (ddlRegions) will be re-bound to the data.

 

Back to top

Example 1 - General Usage

<addform>
  ...
  <table>
    <tr>
      <td>
        <label for="txtFirstName" text="First Name" />
        <textbox id="txtFirstName" datafield="FirstName" datatype="string" />
       </td>
     </tr>
     <tr>
       <td>
         <label for="ddlColors" text="Favorite Color" />
         <dropdownlist id="ddlColors" datafield="FavoriteColor" datatype="string">
           <listitem value="#00FF00">Green</listitem>
           <listitem value="#FF0000" selected="true">Red</listitem>
           <listitem value="#0000FF">Blue</listitem>
         </dropdownlist>

       </td>
     </tr>
     <tr>
       <td colspan="2">
         <addbutton text="Add"/> <cancelbutton text="Cancel"/>
       </td>
     </tr>
   </table>
</addform>

Example 2 - Binding to a Data Source

<AddForm>
  ...
  <ControlDataSource Id="dsColors" CommandText="SELECT ColorId, ColorName FROM MyColorsTable"/>
  ...
  <DropDownList Id="ddlColors" DataSourceId="dsColors" DataTextField="ColorName" DataValueField="ColorId"
     DataField="FavoriteColor" DataType="Int32" />
</AddForm>

Example 3 - Adding Items to a Data-Bound List

This example shows how to use the AppendDataBoundItems property to add a "None Selected" item to a list that is being populated from a table.

<AddForm>
  ...
  <ControlDataSource Id="dsColors" CommandText="SELECT ColorId, ColorName FROM MyColorsTable"/>
  ...
  <DropDownList Id="ddlColors" DataSourceId="dsColors" DataTextField="ColorName" DataValueField="ColorId"
     DataField="FavoriteColor" DataType="Int32" AppendDataBoundItems="True">
    <ListItem Value="-1">(None Selected)</ListItem>
  </DropDownList>
</AddForm>

Example 4 - How to Require an Item Be Selected

This example shows how you can require that the user choose an item from your data-bound list. Notice the Value for the "None Selected" item is set to an empty string. This will be interpreted by the Required Field Validator as not having a value. Presumably the colors you're retrieving from the MyColorsTable table will have values. This type of approach will work for hard-coded lists as well.

<AddForm>
  ...
  <ControlDataSource Id="dsColors" CommandText="SELECT ColorId, ColorName FROM MyColorsTable"/>
  ...
  <DropDownList Id="ddlColors" DataSourceId="dsColors" DataTextField="ColorName" DataValueField="ColorId"
     DataField="FavoriteColor" DataType="Int32" AppendDataBoundItems="True">
    <ListItem Value="">(None Selected)</ListItem>
  </DropDownList>
  <Validate Type="Required" Target="ddlColors" Text="**" Message="Please select a color" />
  ...
  <ValidationSummary Id="vsSummary" DisplayMode="BulletList" />
  ...
</AddForm>
Back to top