2011-10-28 112 views
0
function BindRolesDropdownList() { 
     $.ajax({ 
      type: "Post", 
      url: "Dashboard.aspx/PopulateSelectRoleList", 
      data: "{}", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function(msg) { 

       $('#ddlRoles').get(0).options.length = 0; 
       $("#ddlRoles").get(0).options[0] = new Option("Select Role", "-1"); 

       $.each(msg.d, function(index, item) { 
        $("#ddlRoles").get(0).options[$("#ddlRoles").get(0).options.length] = new Option(item.Display, item.Value); 
       }); 
      } 
     }); 
    } 
    function BindDropdownList() { 
     BindRolesDropdownList(); 
    } 


    [WebMethod] 
      public static ArrayList PopulateSelectRoleList() 
      { 
       //ArrayList lst = new ArrayList(); 
       //lst = DataAccess.DataAccess.GetRolesArrayList(); 
       //for (int i = 0; i < lst.Count; i++) 
       //{ 


       //} 


       //return lst; 
       return new ArrayList() 
       { 

        new { Value = 1, Display = "Male" }, 
        new { Value = 2, Display = "Female" } 
       }; 


      } 

    public static ArrayList GetRolesArrayList() 
      { 
       ArrayList aryList = new ArrayList(); 
       DataSet ds = new DataSet(); 
       ds = DBUtility.SQLExecuteDataset("select * from ST_Roles"); 
       foreach (DataRow row in ds.Tables[0].Rows) 
       { 
        aryList.Add(row); 
       } 
       return aryList; 
      } 

上面選擇的選項列表是它填補我的選擇選項的代碼,我的問題是如何通過ArrayList的迭代,並從數據庫返回的值,而不是在我的代碼部分傳遞的靜態值評論說。填充從ArrayList中

+0

你試過了什麼?你有什麼麻煩? – SLaks

+0

在這裏看:http://stackoverflow.com/questions/7895205/creating-dropdown-selectoption-elements-with-javascript/7895287#7895287 – Sam

+0

我從數據庫的值到arraylist,如果你看看註釋部分下面的代碼有返回類型的ArrayList。我希望我的數據庫中的ArrayList能夠以這種格式返回新的ArrayList() { {value = 1,Display =「Male」}, new {Value = 2,Display =「Female」} }; – Tan

回答

0
[WebMethod] 
public static ArrayList PopulateSelectRoleList() 
{ 
    return DataAccess.DataAccess.GetRolesArrayList() 
    .Select((role, index) => new 
    { 
     Value = index + 1, 
     Display = role 
    }); 
} 
+0

上面在我的選擇列表中沒有顯示任何內容,我已經試過了。我只是想通過ArrayList迭代並將其帶入格式返回新的ArrayList() { {Value = 1,Display =「Male」}, new {Value = 2,Display =「Female」} };如何做到這一點 – Tan

+0

我明白了。請查看我上面編輯的版本。 – weir

+0

沒有運氣,.select沒有顯示 – Tan