0

I am adding a new option to a asp.net dropdown and I am selecting this option. When the page is posted back, the option added and selected via JQuery isn't reflected from the code behind. What could be the issue.

Code below adding the option and selecting:

$("#DdlProductType").prepend($('<option>', { value: 'ALL', text: 'All' }));
$("#DdlProductType").prop('selectedIndex', 0);                

Right before postback, javascript alert shows the correct option selected (ALL, index 0). But when retrieving the selected text and index of DdlProductType from code behind, another item is selected. I'm guessing this has something to do with the viewstate not being updated.

Thanks.

rro
  • 619
  • 5
  • 22
  • Think you need to add the ALL option in your code behind on the initial load. – cgatian Apr 29 '13 at 03:18
  • ALL is in the list on intial load. I am only dynamically adding/removing ALL depending on some conditions. – rro Apr 29 '13 at 03:53
  • 1
    You need to do this from server-side. [Have a look here](http://stackoverflow.com/questions/228969/invalid-postback-or-callback-argument-event-validation-is-enabled-using-page) – mshsayem Apr 29 '13 at 03:59
  • I just found out that disabled controls aren't included in the form post body and confirmed this in fiddler. So this explains why ALL is not set for DDlProductType because I am disabling this control as well. But what's odd is that there are other dropdownlist controls that are disabled as well which are getting updated, yet the their selected values are not in the post body. Does anyone know why this is happening? – rro Apr 29 '13 at 05:38

1 Answers1

0

You are right , asp.net inbuild control uses the viewstate to de serialize it self and populated back in the code behind file . Any item added on the client side do not update the view state because of this , you are not getting that value on the server side. One of the way of doing this is to used the Updatepanel and you add the values of the dropdown on the server side . One of the possible way also( I have not tested fully) is to use the normal dropdown list with run at server true.

Devesh
  • 4,500
  • 1
  • 17
  • 28