Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.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# 通过使用jquery调用代码隐藏方法,在其他dropdownlist发生更改时填充dropdownlist_C#_Jquery_Asp.net_Ajax - Fatal编程技术网

C# 通过使用jquery调用代码隐藏方法,在其他dropdownlist发生更改时填充dropdownlist

C# 通过使用jquery调用代码隐藏方法,在其他dropdownlist发生更改时填充dropdownlist,c#,jquery,asp.net,ajax,C#,Jquery,Asp.net,Ajax,我使用的是Asp.Net/C,我需要在更改另一个dropdownlist时填写dropdownlist,我可以通过将第一个dropdownlist的AutoPostBack属性设置为True,使用SelectedIndexChanged事件来完成,但我有一个密码文本框,在回发时会被清除,所以这个解决方案是不可行的。我决定使用jquery ajax来调用我的代码隐藏方法,但我第一次使用这种方法,所以我不知道该怎么做。这是我迄今为止尝试过的方法,但它对我不起作用 $('#ddlDivisionNam

我使用的是Asp.Net/C,我需要在更改另一个dropdownlist时填写dropdownlist,我可以通过将第一个dropdownlist的AutoPostBack属性设置为True,使用SelectedIndexChanged事件来完成,但我有一个密码文本框,在回发时会被清除,所以这个解决方案是不可行的。我决定使用jquery ajax来调用我的代码隐藏方法,但我第一次使用这种方法,所以我不知道该怎么做。这是我迄今为止尝试过的方法,但它对我不起作用

$('#ddlDivisionName').change(function() {
        alert('j');
        $.ajax({
            type: "POST",
            url: "/CreateAccount/CreateAccount.aspx/BranchNameFill",
            data: "{'index':1}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: true,
            cache: false,
            success: function() {
                alert('ok');
            }
        })
        return false;
    });

[WebMethod]
        public static string BranchNameFill(int index)
        {

            ddlBranchName.Items.Clear();   
            IEnumerable<BRANCH> br;
            br = (from b in dt.BRANCHes
                  where b.DIVNO ==index
                  select b);
            for (int i = 0; i < br.Count(); i++)
            {
                ddlBranchName.Items.Add(br.ElementAt(i).NAME.Trim());
            }





        }

这里有一个简单的例子,我希望它能对你有所帮助

   [WebMethod]
        public List<BRANCH> BranchNameFill(int index)

        {

             br = (from b in dt.BRANCHes
                      where b.DIVNO ==index
                      select b);


            return br.ToList();

        }

    ajax client 
    function getCars() {
        $.ajax({   
          type: "POST",
          url: "/CreateAccount/CreateAccount.aspx/BranchNameFill",       
          data: "{'index':1}",       
          contentType: "application/json; charset=utf-8",        
          dataType: "json",        
          success: function(response) {        
            var branches = response.d;
            $('dropdownlist').empty();  
            $.each(branch, function(index, branches) {     
              $('dropdownlist').append('<option value="' + branch.ID + '" >' + branch.NAME + '</option>');

            });

          },

          failure: function(msg) {



          }

        });

      }

    </script>

最简单的方法是创建一个带有html等选项的字符串,用您的选项填充它,从webmethod返回并将其附加到下拉列表中list@COLDTOLD是的,我知道我必须传递一个数组或类似的东西,然后使用jqueryi必须填充dropdownlist,是吗?是的,您需要从webmethod返回,然后当您有数据时,使用jquery@freebird这些可能对你有帮助
$.ajax({
                type: "POST",
                url: "EBService.aspx/getDepts",
                data: "{cid:" + reg + "}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    $("#<%=ddlCDept.ClientID%>").get(0).options.length = 0;
                        $("#<%=ddlCDept.ClientID%>").get(0).options[0] = new Option("Select name", "0");
                        $("#lblDeptResult").addClass("loading");
                        var i = 0;
                        $.each(msg.d, function (index, item) {
                            $("#<%=ddlCDept.ClientID%>").get(0).options[$("#<%=ddlCDept.ClientID%>").get(0).options.length] = new Option(item.c_dept_name, item.c_dept_id);
                            i = i + 1;
                        });

 [WebMethod]
    public static List<CDept_MstrBO> getDepts(int cid)
    {
        if (CDeptList == null)
        {
            CDept_MstrBO objDeptBo = new CDept_MstrBO();
            objDeptBo.companyID = 1;
            CDeptList = objDeptBo.getCDeptList(objDeptBo);
        }
        List<CDept_MstrBO> deptList = CDeptList.Where(d => d.c_id == cid).ToList();
        return deptList;

    }