0

全部,

如何将 Web 服务方法返回的下拉列表项数组绑定到 ddlSubCategory asp.net 服务器控件?这是我的代码。请参阅 jquery OnSuccess 方法中的注释:

<script type="text/javascript">
    $(document).ready(function () {
        $('#<%=ddlParentCategory.ClientID%>').change(function () {
            var selectedParentCategory = getParentCategorySelectedValue();
var params=  []; 
params[id] = selectedParentCategory;  

          $.ajax({
                              type: "POST",
                              url: "MyWebService.asmx/GetSubCategoryItems",
                              data: params,               
                              contentType: "application/json; charset=utf-8",               
                              dataType: "json",               
                              success: OnSuccess,               
                              error: OnError
           });
                      });

    function OnSuccess(data, status) {
;// need to populate ddlSubCategory <asp:DropDownList on UI now
;// using returned array of drop down list items from the web service
    }        

    function OnError(request, status, error) {
                   alert(request.statusText);
    }

        function getParentCategorySelectedValue() {
            var SelectedVal = $('#<%=ddlParentCategory.ClientID %>').val();
            return SelectedVal;
        }

   });    
</script>

Here is my web service web method returning the array of list items:

[WebMethod]
        public ListItem []  GetSubCategoryItems(int id)
        {
            using (var dc = MyDataContext.Open())
            {
                return dc.SubCategory
                    .OrderBy(sc => sc.Name)
                    .Select(sc => new ListItem()
                                     {
                                         Value = sc.ID.ToString(),
                                         Text = sc.Name
                                     })
                    .Prepend(new ListItem() {Value = "", Text = "Please Select"})
                    .ToArray();
            }
        }

谢谢你的帮助!

4

2 回答 2

1

你可以尝试这样的事情:

function OnSuccess(data, status) {
    // create a variable to the array
    var list = data;
    //get your combobox
    var $select = $('#<%=ddlParentCategory.ClientID %>'); 
    //check if it contains some items
    if (list.length > 0) {
        $select.empty().append('<option value="0">Please select...</option>');
        $.each(list, function () {
            $select.append($("<option></option>").val(this['Value']).html(this['Text']));
        });
        // $select.val(valueselected); //if you want to select a value... add this line
    }
    else //empty result from array
        $select.empty().append('<option selected="selected" value="0">Empty...<option>');
}

我不知道你是从 web 服务中的 web 方法返回你的数组,但你必须考虑这些项目才能让它工作:

  • 您的 Web 服务必须可以通过脚本访问,因此[System.Web.Script.Services.ScriptService]请在您的 Web 服务类上添加属性。
  • 使您的 Web 服务方法可以通过 get 访问(尝试直接从浏览器访问)。您可以web.config 文件上执行类似的操作。
  • 返回json格式,看这个链接
  • 确保您dataOnSuccess函数参数与数组没有属性,例如,data.items
于 2012-07-04T13:02:43.257 回答
1
function OnSuccess(data, status) {
   var array = JSON.parse(data).toArray(); 
   var ddl = $('#<%=ddlParentCategory.ClientID %>');
   for (var item = 0; item < array.length; item++)
   {
      $(ddl).append($("<option>" + item + "</option>"))
   }
}

更多可以在这里找到:http: //www.dotnetfunda.com/articles/article999-dynamically-adding-an-item-in-a-dropdownlist-control.aspx 祝你好运。

于 2012-07-04T13:19:23.913 回答