Arianule
Arianule

Reputation: 9053

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#?

Upvotes: 0

Views: 3125

Answers (2)

AAlferez
AAlferez

Reputation: 1502

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

Upvotes: 1

enzo
enzo

Reputation: 1

Is that an aspx page? If so, add a name attribute to your select and use

Requets.Form["elementName"];

in aspx.cs.

Btw: to your javascript code: is there any particular reason why are you using DOM selection instead of jquery? In jquery you wold just use

var selectedItem = $("#FirstDropDown").find(":selected").text()

Upvotes: 0

Related Questions