asp.net example - dropdownlist default value
The following asp.net c# example code demonstrate us how can we set an item as DropDownList
default selected item (default value).
DropDownList server control's ListItem object has a property named 'Selected'. This property accept a Boolean value. If we set this property value to 'True' then DropDownList will display the specified ListItem object as initially selected in web browser. We can set this 'Selected' property value both declarative way and programmatic way in c# script area. We can set only one item's 'Selected' property value to 'True' at a time because DropDownList control allow only an item as selected item.
DropDownList server control's ListItem object has a property named 'Selected'. This property accept a Boolean value. If we set this property value to 'True' then DropDownList will display the specified ListItem object as initially selected in web browser. We can set this 'Selected' property value both declarative way and programmatic way in c# script area. We can set only one item's 'Selected' property value to 'True' at a time because DropDownList control allow only an item as selected item.
dropdownlist-default-value.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = "you selected....<br />";
Label1.Text += "item: " + DropDownList1.SelectedItem.Text;
Label1.Text += "<br />value: " + DropDownList1.SelectedItem.Value;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net dropdownlist default value</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
asp.net example - dropdownlist default value
</h2>
<hr width="550" align="left" color="Gainsboro" />
<asp:Label
ID="Label1"
runat="server"
Text="select an item from dropdownlist."
Font-Size="X-Large"
Width="350"
>
</asp:Label>
<br /><br />
<asp:DropDownList
ID="DropDownList1"
runat="server"
AutoPostBack="true"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
>
<asp:ListItem Text="Tundra Swan" Value="1"></asp:ListItem>
<asp:ListItem Text="Egyptian Goose" Value="2"></asp:ListItem>
<asp:ListItem Selected="True" Text="African Pygmy Goose" Value="3"></asp:ListItem>
<asp:ListItem Text="Common Shelduck" Value="4"></asp:ListItem>
<asp:ListItem Text="Muscovy Duck" Value="5"></asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
</html>


- How to get DropDownList selected value by selectedindexchanged event
- How to add an item at the top of DropDownList
- How to add an item at DropDownList at index 0
- How to change DropDownList selected item color
- How to make DropDownList first item not selectable
- How to display tooltip for DropDownList each items
- How to change DropDownList item background color
- How to highlight an item in a DropDownList
- How to create a rounded corners DropDownList
- How to change DropDownList selected item text and background color