通过使用jquery调用方法后面的代码来填充更改其他下拉列表的下拉列表

时间:2012-04-21 06:37:22

标签: c# jquery asp.net ajax

我正在使用Asp.Net/C#,我需要填写更改下一个下拉列表的下拉列表,我可以通过将SelectedIndexChanged属性设置为AutoPostBack来使用True事件完成此操作第一个下拉列表,但我有一个密码文本框,在回发时被清除,因此该解决方案不可行。我决定使用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());
            }





        }

2 个答案:

答案 0 :(得分:1)

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

   [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>

答案 1 :(得分:0)

$.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;

    }