两种绑定DropDownList数据源的方式

来源:互联网 发布:js 删除指定的tr 编辑:程序博客网 时间:2024/05/16 07:33

第一种:   (更快)
   
                …………………………………………  
  DataSet   ds=   new   DataSet();      
  ds   =   cls2.SelectOle();  
  DropDownList1.DataSource   =   ds.Tables["My"].DefaultView;          
  DropDownList1.DataTextField   =   "dep_Name";      
  DropDownList1.DataValueField   =   "dep_Id";      
  DropDownList1.DataBind();    
   
  第二种: (更灵活) 
   
  DataSet   set1=   new   DataSet();    
  set1   =   cls2.SelectOle();  
  for   (int   num1   =   0;   num1   <   set1.Tables["My"].Rows.Count;   num1++)  
  {  
  DropDownList1.Items.Add(set1.Tables["My"].Rows[num1][1].ToString());  
  }  
   
  <asp:DropDownList   id="DropDownList1"     DataValueField="dep_Id"   DataTextField="dep_Name"   runat="server"></asp:DropDownList>