问题
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 options from client-side code.
I have implemented cascading dropdownlist function and I used JQuery for that.
At fist I was using AjaxControlToolkit CascadingDropdown control and it can do that but don't know how. any idea?
回答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;
}
来源:https://stackoverflow.com/questions/6805690/client-side-option-append-to-asp-net-dropdownlist-control-and-viewstate-prorblem