我有一个用户控件,当我按下按钮时,它会通过脚本作为对话框打开。
在显示此用户控件之前,我通过对服务器的 ajax 调用填充用户控件包含的两个下拉列表。
我通过 jqyery 填充这两个列表,并将所选索引设置为所需的值。
在用户控件内,我有一个充当“购买”按钮的按钮,两个下拉列表项是产品。
在我的第一个服务器端检查中,我检查两个下拉列表的 selectedindex 是否为 0,并且由于某种原因它总是如此。
Jquery 代码:
success: function (data, textStatus, jqXHR) {
$.each(data.List, function (key, value) {
$("#ctl00_mainAndRightContent_ctl00_SelectResourse_ResourceTypes").append('<option value=' + key + '>' + value + '</option>')
//if only one item is returned
if ($(data.List).length == 1) {
//$("#ctl00_mainAndRightContent_ctl00_SelectResourse_ResourceTypes option:selected").removeAttr("selected");
$("#ctl00_mainAndRightContent_ctl00_SelectResourse_ResourceTypes option:contains('" + value + "')").attr('selected', true).siblings().removeAttr('selected');
$("#ctl00_mainAndRightContent_ctl00_SelectResourse_ResourceTypes").prop('selectedIndex', 1);
}
});
$("#ctl00_mainAndRightContent_ctl00_SelectResourse_EducationTypes").append('<option value =' + data.Id + '>' + 'inserted' + '</option>')
$("#ctl00_mainAndRightContent_ctl00_SelectResourse_EducationTypes").prop('selectedIndex', 0);
$("#ctl00_mainAndRightContent_ctl00_SelectResourse_EducationTypes option").attr('selected', true);}
我在ajax调用后得到了想要的结果,但是当我来到服务器端时 我明白了This :
我在这里缺少什么?
编辑:
我在 firebug 中看到的图片 here
最佳答案
显然,当您通过脚本插入项目时, View 状态就会过时并且不会保存值。
我在这里找到了答案: http://forums.asp.net/t/1745879.aspx?Empty+value+DropDown+when+Postback+after+use+jquery+ajax
关于c# - 为什么我来到服务器代码时Dropdownlist SelectedIndex总是0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23080733/