The DropdownList tag renders as a drop-down list control at run-time.
<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>
<controldatasource>
tag will be appended to the list of items already defined in the control. This only applies if the control is bound to such a tag. The default value is False.<submitcommand>
which will be filled with this control's data on when the form is submitted and/or the parameter in the <selectcommand>
which will supply this control's data when the form is loaded. This attribute is required if the control will participate in operations with your form's data commands.<controldatasource>
tag, specify that tag's ID in this attribute. This attribute is required only if the control's data is supplied via a <controldatasource>
tag.<controldatasource>
this attribute specifies the column name in that datasource that supplies each list item's display text. This attribute is required if the control's data is supplied via a <controldatasource>
tag.<controldatasource>
this attribute specifies the column name in that datasource that supplies each list item's hidden value. This attribute is required only if the control's data is supplied via a <controldatasource>
tag.<listitem>
child tags which define the items that will appear in the list. The control can also be bound to a <controldatasource>
tag. To do so, specify the ID of the <controldatasource>
tag in the control's "datasourceid" attribute, the name of the column in the data source that should supply the display text for each list item, and the column in the data source that should supply the hidden value of each list item.
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" /> <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:
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
<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>
<AddForm>
... <ControlDataSource Id="dsColors" CommandText="SELECT ColorId, ColorName FROM MyColorsTable"/> ... <DropDownList Id="ddlColors" DataSourceId="dsColors" DataTextField="ColorName" DataValueField="ColorId" DataField="FavoriteColor" DataType="Int32" />
</AddForm>
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>
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