we can add its option items by directly writing into .aspx page directly or dynamically add at run time or bind through database.
Following are some important properties that are very useful.
SelectedValue
|
Get the value of the Selected item from the dropdown box.
|
SelectedIndex
|
Gets or Sets the index of the selected item in the dropdown box.
|
SelectedItem
|
Gets the selected item from the list.
|
Items
|
Gets the collection of items from the dropdown box.
|
DataTextField
|
Name of the data source field to supply the text of the items. (No need to set when you are adding items directly into .aspx page.)
|
DataValueField
|
Name of the data source field to supply the value of the items. (No need to set when you are adding items directly into .aspx page.)
|
DataSourceID
|
ID of the datasource component to provide data. (Only used when you have any DataSource component on the page, like SqlDataSource, AccessDataSource etc.)
|
DataSource
|
The datasource that populates the items in the dropdown box. (Generally used when you are dynamically generating the items from Database.)
|
AutoPostBack
|
true or false. If true, the form is automatically posted back to the server when user changes the dropdown list selection. It will also fireOnSelectedIndexChanged method.
|
AppendDataBoundItems
|
true or false. If true, the statically added item (added from .aspx page) is maintained when adding items dynamically (from code behind file) or items are cleared.
|
OnSelectedIndexChanged
|
Method name that fires when user changes the selection of the dropdown box. (Fires only when AutoPostBack=true.)
|
Example:
<asp:DropDownList ID="DropDownList1" runat="server" Height="25px" Width="274px">
<asp:ListItem Value="1">January</asp:ListItem>
<asp:ListItem Value="2">February</asp:ListItem>
<asp:ListItem Value="1">March</asp:ListItem>
</asp:DropDownList>
If you run the web application now, you should see that January, February and March items shown in the DropDownList.
To add items to the DropDownList at run time using C# code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListItem jan = new ListItem("January", "1");
ListItem feb = new ListItem("February", "2");
ListItem mar = new ListItem("March", "2");
DropDownList1.Items.Add(jan);
DropDownList1.Items.Add(feb);
{
if (!IsPostBack)
{
ListItem jan = new ListItem("January", "1");
ListItem feb = new ListItem("February", "2");
ListItem mar = new ListItem("March", "2");
DropDownList1.Items.Add(jan);
DropDownList1.Items.Add(feb);
DropDownList1.Items.Add(mar);
}
}
}
}
If you want a specific listitem to be selected in the dropdownlist, when the page loads, then set the Selected property of the ListItem object to true.
The HTML with the Selected property is shown below.
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="1">Male</asp:ListItem>
<asp:ListItem Value="1" Selected="True">Female</asp:ListItem>
</asp:DropDownList>
A DropDownList is a collection of ListItem objects. Below adding items to these controls is also very similar to DropDownList.
1. CheckBoxList
2. RadioButtonList
3. BulletedList
4. ListBox
1. CheckBoxList
2. RadioButtonList
3. BulletedList
4. ListBox

No comments:
Post a Comment