Client Side option append to asp.net DropDownList Control and ViewState Prorblem

我们两清 提交于 2019-12-11 08:25:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!