0

Hi I have a List which is returned by a webmethod. This needs to be set to a Listbox ? How can this be done ? Thanks in advance

I set the value of 2 labels using this code . Now need to set a listbox as well.

$(document).ready(function() {
    $.ajax({
        type: "POST",
        url: "WebForm2.aspx/GetTime1",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {
            $('#<%=Label2.ClientID %>').html(result.d.Label1);
            $('#<%=Label3.ClientID %>').html(result.d.Label2);
        }
    });
});

This is the object of this class returned by the webmethod

public class StatusViewModel
    {
        public string Label1 { get; set; }
        public string Label2 { get; set; }
        public List<string> ListBox { get; set; }
    }

2 Answers 2

1

If I understood correctly, ListBox are already at the page, let it have server id lbYourListBox

for (var i = 0; i < result.ListBox.length; i++) {  
  $(document.createElement("option")).attr("value",result.ListBox[i]).html(result.ListBox[i])
   .appendTo('#<%=lbYourListBox.ClientID  %>')}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks..Works just fine.. Just needed to add 'd' in result.d.ListBox[i]
1

Try to return a list and then bind your list to your listbox.

 $.each(ListBox, function(index, item) {
                                $("#ListBoxtoBeFilled").get(0).options[$("#ListBoxtoBeFilled").get(0).options.length] = new Option(item);
                             }); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.