簡體   English   中英

JQuery從asmx web服務返回一系列下拉列表項,然后綁定到下拉列表?

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

所有,

如何將從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();
            }
        }

謝謝你的幫助!

你可以嘗試這樣的事情:

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服務必須可以通過腳本訪問,因此在Web服務類上添加[System.Web.Script.Services.ScriptService]屬性。
  • 使您的Web服務方法可以通過get訪問(嘗試直接從瀏覽器訪問)。 你可以做一些像這樣的web.config文件。
  • 返回json格式,看看這個鏈接
  • 確保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>"))
   }
}

更多信息可以在這里找到: 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