綁定到屬性(字段)
綁定語法:<%#字段名或屬性名%>
<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方法進行綁定。