2011-10-28 113 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