Skip to content Skip to sidebar Skip to footer

Client Side Option Append To Asp.net Dropdownlist Control And Viewstate Prorblem

Is there any way of appending options to an asp.net dropdownlist control and viewstate at the same time? My dropdownlist is empty when the page is loaded then I am appending option

Solution 1:

Appending options to a drop down list control will not reflect on the code behind, because you can't change in the view state of the drop down list, I have had the same problem while working with moving options from a drop down list to another and the solution that worked for me is to create another javascript function that loops on the drop down list and takes that values that i want and append them to a hidden field so that i can do my procession on the code behind. In the submit button call this javascript function and you will find the drop down list values as a comma separated in the hidden field.

function SaveList()
{
//Clear the hidden field
var hField =  document.getElementById('<%= YourHiddenField.ClientID %>'); 
hField.value = '' ;

var selectedList = document.getElementById('<%= YourDropDownList.ClientID %>')
for(i = 0; i < selectedList.options.length; ++i)
{         
hField.value = hField.value + ',' + selectedList.options[i].value;
}

Post a Comment for "Client Side Option Append To Asp.net Dropdownlist Control And Viewstate Prorblem"