Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8175299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:43:38+00:00 2026-06-06T22:43:38+00:00

All, How can I bind the array of drop down list items returned from

  • 0

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? Here is my code. See comments in the jquery OnSuccess method:

<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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:43:42+00:00Added an answer on June 6, 2026 at 10:43 pm

    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:

    • your web service has to be accessible by scripts, so add [System.Web.Script.Services.ScriptService] attribute on your web service class.
    • make your web service method to become accessible via get (try to access directly from the browser). You can do something like this on the web.config file.
    • return on the json format, look at this link
    • be sure that your parameter data on the OnSuccess function, has no properties with the array, e.g., data.items
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the jQuery Simple Drop-Down Menu Plugin from http://javascript-array.com/scripts/jquery_simple_drop_down_menu/ In their example, when
I have 2 questions: 1- We all know that can create an array list
I had Repeater which bind method this method retrieve data from data base by
Why all use binary files, if all can use XML ?
I have a table with 5 string columns, all can be NULLs. After I
All I can make out is that one of them is the BC for
All I can find using fopen() and fwrite() in C is to delete all
Hi all I can't find the error in this table creation bit, seems really
Hey all how can i set this up for a loop? data.row9_1 I cant
Although obviously not all scenarios can be covered by a single design, is it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.