asp.net - Select Control Populated from JavaScript Returns Empty Value -


in asp.net applications, have server-side html select control.

<select id="companydropdown" runat="server" style="width:330px"> </select> 

i have link on page runs javascript function populates control, , selects "current" item. can see working because list has items , correct 1 selected.

however, when page posts in response button click.

<asp:button runat="server" id="btnsavecompany" text="save"      onclick="btnsavecompany_click" /> 

companydropdown.value empty, , contains no items. suggestions on how can see selected value on postback?

(note: tried using asp.net dropdown control, fails postback validation when control has items added after page rendered.)

because items added list using javascript aren't part of viewstate, cannot retrieved server side. if trying selected value, put server-side hidden field on page , set value using javascript each time selection of drop-down changed:

jquery script:

$('#companydropdown').change(function() {     $('#inpselectvalue').val($(this).val()); }); 

hidden field markup:

<input runat="server" id="inpselectvalue" clientidmode="static" type="hidden" /> 

the value of hidden field carried server on post back. refer retrieve value of selected item. sure add clientidmode="static" drop down keep rendered id of controls set id assign in aspx page (i.e., companydropdown rather parentcontainer_companydropdown):

<select id="companydropdown" runat="server" style="width:330px"     clientidmode="static" /> 

alternatively, use ajax tell server adding each dynamically generated list item or changing selection. if needed maintain list of items in addition 1 selected.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -