How to programmatically select DropDownList item by item text in asp.net

asp.net dropdownlist select item by text
The following asp.net c# example code demonstrate us how can we select an item programmatically from DropDownList which item's Text match the specified string.

To do this, first we remove the selection of DropDownList item by calling ClearSelection() method. Next we search through the DropDownList items collection to match a specified search string. If we get any item from DropDownList items collection that match the search text then we can set this ListItem's 'Selected' property value to 'True'. ListItem object's Selected property allow us to select the specified item programmatically at run time.

ListItem objects collection FindByText() method allow us to search through the items collection to match search string. FindByText() method search the collection for a ListItem with a Text property that match the search string.
dropdownlist-select-item-by-text.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>    
      
<!DOCTYPE html>
        
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        DropDownList1.ClearSelection();

        //select dropdownlist item by item text. 
        DropDownList1.Items.FindByText("Limpkin").Selected = true;
        
        Label1.Text = "programatically 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 text</title>  
</head>        
<body>        
    <form id="form1" runat="server">        
    <div>        
        <h2 style="color:MidnightBlue; font-style:italic;">        
            asp.net example - dropdownlist select item by text
        </h2>        
        <hr width="550" align="left" color="Gainsboro" />        
        <asp:Label       
            ID="Label1"       
            runat="server"      
            Text="click button to select 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="Corn Crake" Value="1"></asp:ListItem>
            <asp:ListItem Text="Okinawa Rail" Value="2"></asp:ListItem>
            <asp:ListItem Text="Buff-banded Rail" Value="3"></asp:ListItem>
            <asp:ListItem Text="Masked Finfoot" Value="4"></asp:ListItem>
            <asp:ListItem Text="Limpkin" 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 text is 'Limpkin'"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>        
    </form>        
</body>        
</html>
More asp.net examples