asp.net dropdownlist select item by value
This tutorial demonstrate us how can we programmatically select an item from DropDownList server
control's which item's Value property value match the specified search string.
At the beginning, we clear the item selection of DropDownList control. Next, we search through the DropDownList items collection which ListItem object's Value property value match the specified search characters. If we find any item from DropDownList that match search criteria then we can simply select the specified item. ListItem object's Selected property can select an item programmatically at run time. Selected value accept a Boolean value. True value indicate the item is selected.
List items collection FindByValue() method allow us to search through the items collection which ListItem objects have Value property value. If the FindByValue() method find any item that match the search criteria then it return the specified ListItem object. If the method does not find any item then it return null.
At the beginning, we clear the item selection of DropDownList control. Next, we search through the DropDownList items collection which ListItem object's Value property value match the specified search characters. If we find any item from DropDownList that match search criteria then we can simply select the specified item. ListItem object's Selected property can select an item programmatically at run time. Selected value accept a Boolean value. True value indicate the item is selected.
List items collection FindByValue() method allow us to search through the items collection which ListItem objects have Value property value. If the FindByValue() method find any item that match the search criteria then it return the specified ListItem object. If the method does not find any item then it return null.
dropdownlist-select-item-by-value.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
DropDownList1.ClearSelection();
DropDownList1.Items.FindByValue("3").Selected=true;
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 select item by value</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
asp.net example - dropdownlist select item by 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="false"
Width="350"
Font-Size="X-Large"
Font-Names="Comic Sans MS"
>
<asp:ListItem Text="Crested Eagle" Value="1"></asp:ListItem>
<asp:ListItem Text="Indian Black Eagle" Value="2"></asp:ListItem>
<asp:ListItem Text="Greater Spotted Eagle" Value="3"></asp:ListItem>
<asp:ListItem Text="Eastern Imperial Eagle" Value="4"></asp:ListItem>
<asp:ListItem Text="Golden Eagle" Value="5"></asp:ListItem>
</asp:DropDownList>
<br /><br /><br /><br /><br /><br />
<br /><br /><br /><br />
<asp:Button
ID="Button1"
runat="server"
Text="select item which value is '3'"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>


- How to get DropDownList selected value by selectedindexchanged event
- How to set a default value for DropDownList
- How to add an item at the top of DropDownList
- How to add an item at DropDownList index 0
- How to add an item to DropDownList after databind
- How to add a blank item at the top of DropDownList
- How to sort DropDownList items in descending order
- How to select DropDownList item by item text
- How to create a DropDownList displaying years
- How to add vertical space between items in a RadioButtonList