2011-08-09 5 views
0

안녕하세요, 나는 webmethod에 의해 반환되는 목록이 있습니다. 이것을 Listbox로 설정해야합니까? 어떻게 할 수 있습니까? 미리 감사드립니다.jQuery를 사용하여 목록 상자의 값을 설정하는 방법은 무엇입니까?

이 코드를 사용하여 2 개의 라벨 값을 설정했습니다. 이제 목록 상자도 설정해야합니다.

$(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); 
     } 
    }); 
}); 

는 목록을 반환 한 다음 목록 상자에 목록을 바인딩

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

답변

1

나는 목록 상자가이 서버 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 %>')} 
+0

Thanks..Works 잘 .. 그냥 result.d에서 'D'를 추가 필요 .ListBox [i] – Ananth

1

시도의 WebMethod에 의해 반환이 클래스의 객체이다.

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