Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set selected value of a 'Select' HTML control

How can I set the selected value of a Select HTML control from a code-behind file using ASP.NET and C#?

like image 830
Amit Avatar asked Apr 18 '11 08:04

Amit


2 Answers

There are FindByText and FindByValue functions available:

ListItem li = Select1.Items.FindByText("Three");
ListItem li = Select1.Items.FindByValue("3");
li.Selected = true;
like image 159
Muhammad Akhtar Avatar answered Oct 24 '22 16:10

Muhammad Akhtar


HTML:

<select id="selUserFilterOptions" runat="server">
   <option value="1">apple</option>
   <option value="2">orange</option>
   <option value="3">strawberry</option>
</select>

C#:

string fruitId = selUserFilterOptions.Value.ToString();
like image 41
Shanker Kana Avatar answered Oct 24 '22 16:10

Shanker Kana