- DropDownList Web Server Control
in ASP.NET, The DropDownList Web server control can be used to helps users to select a item from a single-selection drop-down list box. The DropDownList control is similar to the ListBox Web server control. but one different is that user can select only one item at a time.
The Article represents that how to work with the DropDownList control. Because the control is similar in many ways to the ListBox control, some of the links lead to topics for that control.
Set the DropdownList styles
You are able to control the appearance of the DropDownList control by setting its height and width in pixels.in Some browsers, the height and width in pixels is not working and will use the row-count setting instead. You cannot specify the number of items that are displayed in the list when users click the drop-down button. The length of the displayed list is determined by the browser.
- Add Items in List
When you add a list item, you must specify the three properties of the item as,
- Text - The text displayed in the list.
- Value -The value associated with an item. Setting this property allows you to associate a value with a specific item without displaying it.
- Selected -A Boolean value indicating whether the item is selected.
- To add items at design time
When you add a list item, you must specify the three properties of the item as,
- Type an <asp:ListItem> element in the list control.
- Set the Text and Value properties of the new list item.
- Optionally, set the Selected property for one items.
the following code snippets demonstrates a listbox control
-
<asp:ListBox ID="ListBox1" runat="server">
<asp:ListItem Text="Apple" Value="App" Selected="True" />
<asp:ListItem Text="Orange" Value="Ora" />
<asp:ListItem Text="Banana" Value="Ban" />
</asp:ListBox>
-
- To add items programmatically
- Create a new object of type ListItem and set its Text and Value properties.
- Call the Add method of the control's Items collection and pass it the new object.
the following code snippets demonstrates this
-
Protected void Button1_Click (object sender, System.EventArgs e)
{
ListBox1.Items.Add(new ListItem("Apple", "App"));
ListBox1.Items.Add(new ListItem("Orange", "Ora"));
ListBox1.Items.Add(new ListItem("Banana", "Ban"));
}
-
- To add items programmatically via Data Source
- set the control's AppendDataBoundItems property to true.
- Add a data source control to the page, such as a SqlDataSource or ObjectDataSource control. For details, see Data Source Controls Overview.
- Configure the data source control with connection string and query information.
- Set the list control's DataSourceID property to the ID of the data source control.
- Set the Properties such as DataTextField and DataTextFormatString
the following code snippets demonstrates this
-
<asp:ListBox ID="ListBox1" runat="server"
DataSourceID="SqlDataSource1"
DataTextField="CategoryName"
DataValueField="CategoryID">
</asp:ListBox>
<asp:SqlDataSource
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
ID="SqlDataSource1"
runat="server"
SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories]">
</asp:SqlDataSource>
-
- Related Links
-
7534c885-1b38-4041-a7f8-60281cd82d28|0|.0