简体   繁体   English

JQuery从asmx web服务返回一系列下拉列表项,然后绑定到下拉列表?

[英]JQuery return an array of drop down list items from asmx web service and then bind to drop down list?

All, 所有,

How can I bind the array of drop down list items returned from the web service method to the ddlSubCategory asp.net server control? 如何将从Web服务方法返回的下拉列表项数组绑定到ddlSubCategory asp.net服务器控件? Here is my code. 这是我的代码。 See comments in the jquery OnSuccess method: 请参阅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();
            }
        }

thanks for any help! 谢谢你的帮助!

You can try something like this: 你可以尝试这样的事情:

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>');
}

I don't know hwo you are returning your array from the web method in the web service, but you have to consider this items to get it work: 我不知道你是从web服务中的web方法返回你的数组,但你必须考虑这些项目才能使它工作:

  • your web service has to be accessible by scripts, so add [System.Web.Script.Services.ScriptService] attribute on your web service class. 您的Web服务必须可以通过脚本访问,因此在Web服务类上添加[System.Web.Script.Services.ScriptService]属性。
  • make your web service method to become accessible via get (try to access directly from the browser). 使您的Web服务方法可以通过get访问(尝试直接从浏览器访问)。 You can do something like this on the web.config file. 你可以做一些像这样的web.config文件。
  • return on the json format, look at this link 返回json格式,看看这个链接
  • be sure that your parameter data on the OnSuccess function, has no properties with the array, eg, data.items 确保OnSuccess函数上的参数data没有数组属性,例如data.items
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>"))
   }
}

More can be found here : http://www.dotnetfunda.com/articles/article999-dynamically-adding-an-item-in-a-dropdownlist-control.aspx Good luck. 更多信息可以在这里找到: http//www.dotnetfunda.com/articles/article999-dynamically-adding-an-item-in-a-dropdownlist-control.aspx祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM