jQuery ajax方法绑定Dropdownlist

过程

  1. 前台拖一个DropDownList 控件  
  2. 为其绑定数据调用后面的ajax方法、  
    1. 注意 data 的格式,json有标准,写错了不调用
    2. 前后参数传递要对应,名字一致,个数一致
    3. susscess回调函数 返回的(data)是object ,可以alert 一下试试,如果返回json 需要通过data.d获取json
  3. 在后台cs 文件需引用System.Web.Services;

 

 

 <td height="22" width="10%" align="right">
                        联系人:
                    </td>
                    <td>
                        <asp:DropDownList ID="ddlVContact" Style="width: 140px" class="input-Add" runat="server">
                        </asp:DropDownList>
                        
                    </td>

 

$.ajax({
                            url: 'SourcingInfo.aspx/GetVenderContract',
                            type: 'post',
                            async: true,
                            cache: false,
                            datatype: 'json',
                            contentType: "application/json;charset=utf-8",
                            data: "{VenderID:" + $("#hfVenderID").val() + "}",
                            success: function (result) {
                                var list = eval(result.d);
                                if ($(list).length > 0) {
                                    $.each(list, function (i, item) {
                                        var opt = $("<option></option>").text(item.Cname).val(item.ID);
                                        ddl.append(opt);
                                    })
                                }
                            },
                            error: function (result) {
                                art.dialog({
                                    content: result.responseText
                                });
                            }
                        })
                        //ajax end
    [WebMethod]
    public static string GetVenderContract(string VenderID)
    {
        BLL_Vender_Contact bll = new BLL_Vender_Contact();
        List<Object_Vender_Contact> list = bll.GetListByFilter("AND Vender_id=" + VenderID);
        if(list.Count>0)
        {
            return new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(list);
        }
        else
        {
            return null;   
        }
    }

 

posted @ 2013-03-06 23:20  天北涯  阅读(1506)  评论(0编辑  收藏  举报