Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当我访问服务器代码时,为什么Dropdownlist SelectedIndex总是0_C#_Javascript_Jquery_Asp.net - Fatal编程技术网

C# 当我访问服务器代码时,为什么Dropdownlist SelectedIndex总是0

C# 当我访问服务器代码时,为什么Dropdownlist SelectedIndex总是0,c#,javascript,jquery,asp.net,C#,Javascript,Jquery,Asp.net,我有一个用户控件,当我按下按钮时,它会通过脚本以对话框的形式打开 在显示这个usercontroled之前,我通过对服务器的ajax调用填充usercontrol包含的两个dropdownlists 我通过jqyery填充这两个列表,并将所选索引设置为desierd值 在usercontrol中,我有一个按钮作为“购买”按钮,两个dropdownlists项就是产品 在我的第一次服务器端检查中,我检查两个dropdownlists的selectedindex是否为0,并且出于某种原因,它始终为0

我有一个用户控件,当我按下按钮时,它会通过脚本以对话框的形式打开

在显示这个usercontroled之前,我通过对服务器的ajax调用填充usercontrol包含的两个dropdownlists

我通过jqyery填充这两个列表,并将所选索引设置为desierd值

在usercontrol中,我有一个按钮作为“购买”按钮,两个dropdownlists项就是产品

在我的第一次服务器端检查中,我检查两个dropdownlists的selectedindex是否为0,并且出于某种原因,它始终为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);}
success:函数(数据、文本状态、jqXHR){
$.each(data.List、函数(键、值){
$(“#ctl00_main和right content_ctl00_selectresource_ResourceTypes”)。追加(“”+value+“”)
//如果只返回一个项目
if($(data.List).length==1){
//$(“#ctl00_main and right content_ctl00_selectResources_ResourceTypes option:selected”)。removeAttr(“selected”);
$(“#ctl00_main和right content_ctl00_selectresource_ResourceTypes选项:包含(“+value+”)”)”).attr('selected',true.).sibbins().removeAttr('selected');
$(“#ctl00_main and right content_ctl00_selectresource_ResourceTypes”).prop('selectedIndex',1);
}
});
$(“#ctl00_main和right content_ctl00_selectresource_EducationTypes”).append(“+”插入“+”)
$(“#ctl00_main and right content_ctl00_selectresource_EducationTypes”).prop('selectedIndex',0);
$(“#ctl00_main和right content_ctl00_selectresource_EducationTypes option”).attr('selected',true);}
在ajax调用之后,我得到了desierd结果,但是在服务器端 我明白了:

我错过了什么

编辑:


我在firebug中看到的图片请检查这些下拉列表是否有

runat=server

显然,通过脚本插入项目时,viewstate将过时,并且不会保存值

我在这里找到了答案:

如果你查看图片并阅读完整的问题,你会发现这个答案完全超出了范围。当然,他们设置了runat=“server”标记。