Reputation: 872
I'm trying to get my selected value/text from and HTML select option but I can't get it! I already tried with answers posted in this website but none works.
This my code:
<select id="Select" name="Select" runat="server">
<option value="1">Option 1</option>
</select>
I tried these C# codes:
//With this I get Index out of range error message:
string name = nombre.Items[nombre.SelectedIndex].Text;
//With this I get nothing:
string name = this.nombre.Value.ToString();
//Nothing here:
string name = this.nombre.Value;
What can I do? It's necessary using html select control.
Upvotes: 3
Views: 30930
Reputation: 86
to get a value,
string sVal = dropdownName.Items[dropdownName.SelectedIndex].Value;
to get a text,
string sText= dropdownName.Items[dropdownName.SelectedIndex].Text;
Upvotes: 2
Reputation: 26
You would probably be better off using an asp:DropDownList, but you can still reference straight selects. Here's code that will grab the value and text from your HTML above:
string value = Select.Items[Select.SelectedIndex].Value;
string text = Select.Items[Select.SelectedIndex].Text;
Be sure you're using the correct ID name (Select is shown in your html, but nombre is used in your code behind).
Upvotes: 0