DropDownList – highlighting an item by value (or text)

I had a DropDownList in an ASP.NET page that held the country values, such as this:

 <asp:DropDownList ID="ddlCountry" runat="server">
                <asp:ListItem Text="United States">United States</asp:ListItem>
                <asp:ListItem Text="Canada">Canada</asp:ListItem>
</asp:DropDownList>

 The user object held a property called Country. It held – brace yourself – the country where the user lived! I wanted the DropDownList to display the value of the user’s country. After I instantiated (love that word) the user object, I simply called the FindByValue method of the DDL’s Items collection, like so:

 ddlCountry.Items.FindByValue(TheUser.Country).Selected = true;
Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: