ASP.Net中的關(guān)于數(shù)據(jù)綁定
ASP.Net中的關(guān)于數(shù)據(jù)綁定1. 綁定標(biāo)簽 <%# …… %> 可綁定標(biāo)簽屬性值,表達(dá)式,方法的結(jié)果及數(shù)據(jù)源等。
a. 綁定屬性之label的Text屬性值 :
<asp: label1 ID="labtxt" Text="<%# DataTime.Now.ToShortString() %>" >
this.label1.DataBind();***************后臺(tái)代碼千萬不能少
b.綁定變量
后臺(tái)代碼類中定義一變量 protected Color bgcolor=Color.FromA#b48cd2;
前臺(tái) <asp: label1 ID="labtxt" BackColor="<%# bgcolor %>" runat="server">
this.labtxt.DataBind();
c.綁定常量
后臺(tái) protected int a=15;
protected int b=10;
<td> a+b=<%# a+b %> <td>
this.DataBind();
d.綁定 方法
在后面寫個(gè)方法 protected string GetDate(){……}
在前臺(tái) <td> 今天:<%# GetData() %> </td>
this.DataBind();
e.綁定 數(shù)組集合
Protected string[] strarray={"acb","def","xyz"};
< asp: DropDownList DataSource="<%# strarray %>" >
this.DataBind();
ASP.Net綁定數(shù)據(jù)源
1.使用DropDownList手動(dòng)綁定數(shù)據(jù)源
public void GetBinds()
{
DataTable tb= GetTable(……);
this.DropDownList.DataSource=tb;
this.DropDownList.DataTextField="類別"; //可見值
this.DropDownList.DataValueField="ID" //隱藏值
}
調(diào)用該 方法后記的 加上 this.DataBind();
2. 篇?dú)v的 ChickBoxList
for (int i=0; i<checkboxlist1.Items.Count; i++)
{
if (checkboxlist1.Items[i].Selected)
{
Label1.Text += checkboxlist1.Items[i].Text + "<br />";
}
}
posted on 2012-09-16 14:02 ** 閱讀(135) 評(píng)論(0) 編輯 收藏