绑定到属性(字段)
绑定语法:<%#字段名或属性名%>
<body style=‘<%#bodystyle%>’>…</body>
protected string bodystyle;
protected void Page_Load(object sender, EventArgs e)
{
bodystyle = "background-color:#ff0000";
}
数据绑定
protected void Page_Load(object sender, EventArgs e)
{
string str = "server=PC2011083011PDI\\SQLEXPRESS;database=province;integrated security=sspi";
DataSet ds = new DataSet();
using (SqlConnection sqlCnn=new SqlConnection())
{
sqlCnn.ConnectionString = str;
SqlCommand sqlcmm = sqlCnn.CreateCommand();
sqlcmm.CommandText = "select id,name from province";
SqlDataAdapter da = new SqlDataAdapter(sqlcmm);
da.Fill(ds);
}
this.DropDownList1.DataSource=ds.Tables[0];
this.DropDownList1.DataTextField = "name";
this.DropDownList1.DataValueField = "id";
//this.DropDownList1.DataBind();
this.RadioButtonList1.DataSource = ds.Tables[0];
this.RadioButtonList1.DataTextField = "name";
this.RadioButtonList1.DataValueField = "id";
//this.RadioButtonList1.DataBind();
this.CheckBoxList1.DataSource = ds.Tables[0];
this.CheckBoxList1.DataTextField = "name";
this.CheckBoxList1.DataValueField = "id";
this.GridView1.DataSource = ds.Tables[0];
this.DataBind();
}
使用DropDownList控件绑定
使用DataSource属性指定集合数据源,使用DataBind方法进行绑定。