乏mily

导航

DropDownList 绑定方法总结

1. 基础数据绑定:直接用ListItem枚举,如:

<asp:DropDownList ID="DDL_1" runat="server"> 
  <asp:ListItem Value="1">1</asp:ListItem> 
  <asp:ListItem Value="2">2</asp:ListItem> 
  <asp:ListItem Value="3">3</asp:ListItem> 
  <asp:ListItem Value="4">4</asp:ListItem> 
</asp:DropDownList>

2. 动态绑定:

方法一:绑定数据库中的字段
.....(前面省略)
string ConnString = ConfigurationSettings.AppSettings["SqlConnString"]; using(SqlConnection Conn = new SqlConnection(ConnString)) //使用using不用考虑关闭连接,注销对象
{
  string strSQL="select * from tb_user";
  using(SqlDataAdapter adapter = new SqlDataAdapter(strSql,Conn))
  {
    using(DataSet ds = new DataSet())
    {
      adapter.Fill(ds,"tb_user");

      DDL_1.DataSource
= ds.Tables["tb_user"].DefaultView;
      DDL_1.DataValueField = ds.Tables["tb_user"].Columns[0].ColumnName;
      DDL_1.DataTextField = ds.Tables["tb_user"].Columns[1].ColumnName;
      DDL_1.DataBind();
    }
  }
}
方法二:利用DropDownList.Items.Add方法
.....(前面省略)
if (!IsPostBack) 
{
  string ConnString = ConfigurationSettings.AppSettings["SqlConnString"];              
  SqlConnection Conn = new SqlConnection(ConnString);             
  try             
  {  
    Conn.Open();
       this.DropDownList3.Items.Add("请选择");             
    string strSQL = "select UserName from tb_User";             
    SqlCommand com = new SqlCommand(strSQL, Conn);
    SqlDataReader dr = com.ExecuteReader();             
    while(dr.Read())             
    {
      this.DropDownList3.Items.Add(dr["UserName"].ToString());             
    }             
  }         
  catch (Exception ex)             
  {           
    Response.Write("<scirpt>alert('" + ex.Message.ToString() + "')</script>");             
  }             
  finally             
  {
    Conn.Close();   //此处要考虑关闭连接         
  }             
}
 
注意:
<select id="A">
  <option> 1 </option>
  <option> 2 </option>
  <option> 3 </option>
</select>

 3. 属性介绍

  3.1 DataSource,获取或设置对象,数据绑定控件从该对象中检索其数据项列表(继承自BaseDataBoundControl)
  3.2 DataSourceID,获取或设置对象,数据绑定控件从该对象中检索其数据项列表(继承自DataBoundControl)
  3.3 DataTextField,获取或设置为列表项提供文本内容的数据源字段(继承自ListControl)
  3.4 DataTextFormatString获取或设置格式化字符串,该字符串用来控制如何显示绑定到列表控件的数据(继承自ListControl)
  3.5 DataValueField,获取或设置为列表项提供值的数据源字段(继承自ListControl)
Items,获取列表控件项的集合(继承自ListControl)
  3.6 SelectArgyments,获取数据绑定控件从数据源控件检索数据时使用的DataSourceSelectArguments对象(继承自DataBoundControl)
  3.7 SelectedIndex,获取或设置DropDownList控件中的选定项的索引(重写ListControl.SelectedIndex)
  3.8 SelectedItem,获取列表控件中索引最小的选定项(继承自ListControl)
  3.9 SelectedValue,获取列表控件中选定项的值,或选择列表控件中包含指定值的项(继承自ListControl)
4. 事件介绍
  4.1 DataBinding,当服务器控件绑定到数据源时发生(继承自Control)
  4.2 DataBinding,在服务器控件绑定到数据源后发生(继承自BaseDataBoundControl)
  4.3 SelectedIndexChanged,当列表控件的选定项在信息发往服务器之间变化时发生(继承自ListControl)
  4.4 TextChanged,当Text和SelectedValue属性更改时发生(继承自ListControl)
 
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
5. 绑定选中项
      // ...
     DropDownList1.DataSource = ddlDt; DropDownList1.DataTextField = "orderCompany"; DropDownList1.DataValueField = "id"; DropDownList1.DataBind();      //文本选定项设置 DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(line.express)); /* 重要 */

 

 

posted on 2013-09-10 10:35  乏mily  阅读(547)  评论(0)    收藏  举报