Search code examples
c#javascriptdrop-down-menucode-behind

Obtain selected value from dropdown in code behind


I have the follwing dropdown list and to get the selected value with javascript is easy enough.

<select id="FirstDropDown" runat="server" onchange="ValidatePrimaryDropDown();" > 
                    <option>[Please Select Yes Or No]</option>
                    <option>Yes</option>
                    <option>No</option>
                </select>

var e = document.getElementById("FirstDropDown");
var dropDownFirst = e.options[e.selectedIndex].value;

I prefer to use this dropdown as apposed to 'asp:DropDownList'.

How can I retrieve the selected value in code behind C#?


Solution

  • There are FindByText and FindByValue function available.

    ListItem li = Select1.Items.FindByText("Three");
    ListItem li = Select1.Items.FindByValue("3");
    li.Selected = true;
    

    Link to source