2

我正在使用Asp.Net/C#,我需要在更改另一个下拉列表时填写一个下拉列表,我可以通过将第一个下拉列表的属性SelectedIndexChanged设置为使用事件来完成它,但是我有一个密码文本框,它在回发时被清除,因此该解决方案不可行.我决定用我的电话,但我是第一次使用这种方法,所以我不知道我该怎么做。这是我到目前为止尝试过的,但它对我不起作用。AutoPostBackTruejquery ajaxcode 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());
            }





        }
4

2 回答 2

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>
于 2012-04-21T07:05:31.640 回答
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;

    }
于 2012-12-09T09:09:08.867 回答