Pergunta

I have a custom HTML drop down list that used it in ASP.NET project.I want to get the value of this control if user select any item and pass it to database.

<select>
       <option tabindex="2" id="Option0" runat="server" value="0" selected="selected">انتخاب کنید ... </option>
       <option tabindex="2" id="Option1" runat="server" value="1">یک ساله </option>
       <option tabindex="2" id="Option2" runat="server" value="2">دو ساله</option>
       <option tabindex="2" id="Option3" runat="server" value="3">سه ساله</option>
       <option tabindex="2" id="Option4" runat="server" value="4">چهار ساله</option>
       <option tabindex="2" id="Option5" runat="server" value="5">پنج ساله</option>
       <option tabindex="2" id="Option6" runat="server" value="6">شش ساله</option>
       <option tabindex="2" id="Option7" runat="server" value="7">هفت ساله</option>
       <option tabindex="2" id="Option8" runat="server" value="8">هشت ساله</option>
       <option tabindex="2" id="Option9" runat="server" value="9">نه ساله</option>
       <option tabindex="2" id="Option10" runat="server" value="10">ده ساله</option>
       <option tabindex="2" id="Option11" runat="server" value="0">یازده ساله</option>
       <option tabindex="2" id="Option12" runat="server" value="1">دوازده ساله</option>
       <option tabindex="2" id="Option13" runat="server" value="2">سیزده ساله</option>
       <option tabindex="2" id="Option14" runat="server" value="3">چهارده ساله</option>
       <option tabindex="2" id="Option15" runat="server" value="4">پانزده ساله</option>
       <option tabindex="2" id="Option16" runat="server" value="5">شانزده ساله</option>
       <option tabindex="2" id="Option17" runat="server" value="6">هفده ساله</option>
       <option tabindex="2" id="Option18" runat="server" value="7">هجده ساله</option>
       <option tabindex="2" id="Option19" runat="server" value="8">نوزده ساله</option>
       <option tabindex="2" id="Option20" runat="server" value="9">بیست ساله</option>
   </select>    

how can i understand which item was selected and get the value of this list "if that value was selected"?

Foi útil?

Solução

put runat in select rather than option.

<select id="ddId" runat="server">
</select>

And use SelectedValue to get the value as follows.

string selectedValue= ddId.SelectedValue;

Outras dicas

I can think of 2 ways:

  1. Make this control a server side control and on the selected index changed, you can catch the selected item on the server side.
  2. If you can not make it a server side control then have a hidden variable that gets populated with the selected index of this select. This population of the hidden field can be done via a js function called from the select's onindexchanged event.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top