从Ajax WebMethod填充ComboBox(ajaxToolkit)

时间:2016-05-20 09:27:50

标签: c# asp.net combobox webmethod

在我的ASPX页面上,我有以下ComboBox,应该从Ajax WebMethod填充。

<ajaxToolkit:ComboBox ID ="cbMembers" runat="server"></ajaxToolkit:ComboBox>

填充ComboBox的WebMethod调用如下:

$.ajax({
        type: "POST",
        url: functions.aspx/members",
        data: "{SearchInput: '" + SearchInput + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (r) {                
            var cbMembers = $("[id*=cbMembers]");
            $.each(r.d, function () {
                cbMembers.append( -- My problem is here -- );
            })                
        }
    });

的WebMethod

List<ListItem> members = new List<ListItem>();
...
if (Reader.HasRows)
            { 
                while (Reader.Read())
                {
                    members.Add(new ListItem
                    {
                        Value = HttpUtility.HtmlEncode((string)(Reader["name"])),
                        Text = HttpUtility.HtmlEncode((string)(Reader["name"]))
                    });
                } 
            }
            return members;
...

从WebMethod正确检索数据。我测试了它。但我的问题是填充ComboBox中的列表项。有什么建议?

 success: function (r) {                
            var cbMembers = $("[id*=cbMembers]");
            $.each(r.d, function () {
                cbMembers.append( -- How to append the data here? --);
            })

        }

2 个答案:

答案 0 :(得分:0)

试试这个

$.each(r.d, function () {
cbMembers.append($("<option>",{value:"1",text:"Gloria"}));
})

答案 1 :(得分:0)

试试这个

        $.each(r.d, function () {
            cbMembers.append($("<option></option>").val(this['Value']).html(this['Text']));
        }