通过使用 jquery 调用代码隐藏方法,在更改其他下拉列表时填充下拉列表

本文关键字:下拉列表 其他 填充 方法 jquery 调用 隐藏 代码 | 更新日期: 2023-09-27 18:31:54

我正在使用Asp.Net/C#,我需要在更改另一个下拉列表时填写一个下拉列表,我本可以通过将AutoPostBack属性设置为第一个下拉列表True来使用SelectedIndexChanged事件来完成,但是我有一个密码文本框,该文本框在回发时被清除,因此该解决方案不可行。我决定使用jquery ajax来调用我的code behind method,但我是第一次使用这种方法,所以我不知道该怎么做。这是我到目前为止尝试过的,但它对我不起作用。

$('#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());
            }


        }

通过使用 jquery 调用代码隐藏方法,在更改其他下拉列表时填充下拉列表

这里有一个简单的例子,希望它以某种方式帮助你

   [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>
$.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;
    }
相关文章: