ASP.Net中的關于數據綁定
ASP.Net中的關于數據綁定1. 綁定標簽 <%# …… %> 可綁定標簽屬性值,表達式,方法的結果及數據源等。
a. 綁定屬性之label的Text屬性值 :
<asp: label1 ID="labtxt" Text="<%# DataTime.Now.ToShortString() %>" >
this.label1.DataBind();***************后臺代碼千萬不能少
b.綁定變量
后臺代碼類中定義一變量 protected Color bgcolor=Color.FromA#b48cd2;
前臺 <asp: label1 ID="labtxt" BackColor="<%# bgcolor %>" runat="server">
this.labtxt.DataBind();
c.綁定常量
后臺 protected int a=15;
protected int b=10;
<td> a+b=<%# a+b %> <td>
this.DataBind();
d.綁定 方法
在后面寫個方法 protected string GetDate(){……}
在前臺 <td> 今天:<%# GetData() %> </td>
this.DataBind();
e.綁定 數組集合
Protected string[] strarray={"acb","def","xyz"};
< asp: DropDownList DataSource="<%# strarray %>" >
this.DataBind();
ASP.Net綁定數據源
1.使用DropDownList手動綁定數據源
public void GetBinds()
{
DataTable tb= GetTable(……);
this.DropDownList.DataSource=tb;
this.DropDownList.DataTextField="類別"; //可見值
this.DropDownList.DataValueField="ID" //隱藏值
}
調用該 方法后記的 加上 this.DataBind();
2. 篇歷的 ChickBoxList
for (int i=0; i<checkboxlist1.Items.Count; i++)
{
if (checkboxlist1.Items[i].Selected)
{
Label1.Text += checkboxlist1.Items[i].Text + "<br />";
}
}