ASP.NET Syntax ASP.NET Syntax for Web Controls
Enables users to select from a single-selection drop-down list. The drop-down portion can contain any number of list items.
Declarative Syntax
<asp:DropDownList
AccessKey = "string"
AppendDataBoundItems = "True | False"
AutoPostBack = "True | False"
BackColor = "color name | #dddddd"
BorderColor = "color name | #dddddd"
BorderStyle = "NotSet | None | Dotted | Dashed | Solid | Double | Groove |
Ridge | Inset | Outset"
BorderWidth = size
CausesValidation = "True | False"
CssClass = "string"
DataMember = "string"
DataSource = "string"
DataSourceID = "string"
DataTextField = "string"
DataTextFormatString = "string"
DataValueField = "string"
Enabled = "True | False"
EnableTheming = "True | False"
EnableViewState = "True | False"
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"
OnDataBinding = "DataBinding event handler"
OnDataBound = "DataBound event handler"
OnDisposed = "Disposed event handler"
OnInit = "Init event handler"
OnLoad = "Load event handler"
OnPreRender = "PreRender event handler"
OnSelectedIndexChanged = "SelectedIndexChanged event handler"
OnTextChanged = "TextChanged event handler"
OnUnload = "Unload event handler"
runat = "server"
SelectedIndex = integer
SelectedValue = "string"
SkinID = "string"
Style = "string"
TabIndex = integer
ToolTip = "string"
ValidationGroup = "string"
Visible = "True | False"
Width = size
>
<asp:ListItem
Enabled = "True | False"
Selected = "True | False"
Text = "string"
Value = "string"
/>
</asp:DropDownList>
For information on the individual members of this class, see DropDownList in the class library.
The DropDownList control renders a single selection drop-down list control. You can control the appearance of the DropDownList control by setting the BorderColor, BorderStyle, and BorderWidth properties.
To specify the items that you want to appear in the DropDownList control, place a ListItem element for each entry between the opening and closing tags of the DropDownList control.
The DropDownList control also supports data binding. To bind the control to a data source, first create a data source, such as a System.Collections.ArrayList object, that contains the items to display in the control. Next, use the Control.DataBind method to bind the data source to the DropDownList control. The DropDownList control will now display the information from the data source.
Use the SelectedIndex property to programmatically determine the index of the item selected by the user from the DropDownList control.
The below code snippet demonstrates how to use the DropDownList control.
<html>
<head>
<script language="C#" runat="server">
void Button_Click ( Object sender, EventArgs e ) {
msgLabel.Text = "You selected: " +
DropDownList1.SelectedItem.Text + ".";
}
</script>
</head>
<body>
<form runat="server">
<h3>DropDownList Example</h3>
Select an item from the list and click the submit button.
<p>
<asp:DropDownList id="DropDownList1" runat="server">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem>Item 4</asp:ListItem>
</asp:DropDownList>
<br><br>
<asp:Button id="Button1"
Text = "Submit"
onClick = "Button_Click"
runat="server" />
<br><br>
<asp:Label id="msgLabel" runat="server" />
</form>
</body>
</html>
<html>
<head>
<script language="VB" runat="server">
Sub Button_Click ( sender As Object, e As EventArgs )
msgLabel.Text = "You selected " & _
DropDownList1.SelectedItem.Text & "."
End Sub 'Button_Click
</script>
</head>
<body>
<form runat="server">
<h3>DropDownList Example</h3>
Select an item from the list and click the submit button.
<p>
<asp:DropDownList id="DropDownList1" runat="server">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem>Item 4</asp:ListItem>
</asp:DropDownList>
<br><br>
<asp:Button id="Button1"
Text = "Submit"
onClick = "Button_Click"
runat="server" />
<br><br>
<asp:Label id="msgLabel" runat="server" />
</form>
</body>
</html> |
|
C# |
VB |
DropDownList Class DropDownList Web Server Control