在網頁上呼叫 DataBind 方法時,資料繫結運算式會在 ASP.NET 網頁上任何屬性 (包括伺服器控制項屬性) 與資料來源之間建立繫結。您可以將資料繫結運算式包括在伺服器控制項開頭標記中屬性/值配對中值的那一端,或是網頁上的任何位置。
<tagprefix:tagname property="<%# data-binding expression %>"runat="server" />
或
literal text <%# data-binding expression %>
屬性
- property
對它宣告這個資料繫結的控制項屬性。 - data-binding expression
符合<備註>章節中所列需求的任何運算式。
備註
所有資料繫結運算式 (不論是放在任何位置) 都必須包含在 <%# 和 %> 字元中。
ASP.NET 支援階層式資料繫結模型,這種模型可支援伺服器控制項屬性與父資料來源之間的關聯繫結。任何伺服器控制項屬性都可以對容納它的網頁,或伺服器控制項直接命名容器 (Container) 上的任何公用欄位或屬性資料繫結。
使用 DataBinder.Eval
ASP.NET 提供了一個稱為 DataBinder.Eval 的靜態方法,它會評估晚期繫結的資料繫結運算式,並且將結果選擇性地格式化成為字串。這個方法省去許多強制將值轉換成您想要的資料型別時,所必須執行的明確轉型 (Casting)。
例如,在以下程式碼片段中整數會顯示為貨幣字串。使用標準 ASP.NET 資料繫結語法時,您必須先轉換資料列的型別,才能擷取資料欄位 IntegerValue。接下來,它會被當作 String.Format 方法的引數傳遞:
<%# String.Format("{0:c}", ((DataRowView)Container.DataItem)["IntegerValue"]) %>
與 DataBinder.Eval 的語法相比,這個語法只有三個引數:資料項目的命名容器、資料欄位名稱和格式字串。在樣板化的清單 (如 DataList 類別、DataGrid 類別或 Repeater 類別) 中,命名容器必定是 Container.DataItem。
<%# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:c}") %>
格式字串引數則是選擇項。如果省略的話,DataBinder.Eval 會傳回物件型別的值,如以下範例所示:
<%# (bool)DataBinder.Eval(Container.DataItem, "BoolValue") %>
如果資料繫結控制項在樣板化的清單內,DataBinder.Eval 會特別有用,因為資料列和資料欄位兩者時常都必須轉換。
範例
下列範例展示如何根據 ASP.NET 伺服器控制項中的屬性繫結資料。當使用者從 DropDownList Web 伺服器控制項中選取狀態時,Label Web 伺服器控制項便會根據清單中選取的項目進行繫結。
<html>
<head>
<script language="C#" runat="server">
void SubmitBtn_Click(Object sender, EventArgs e) {
// Rather than explictly pull out the variable from the StateList
// and then manipulate a label control, just call Page.DataBind.
// This will evaluate any <%# %> expressions within the page.
Page.DataBind();
}
</script>
</head>
<body>
<h3><font face="Verdana">Data binding to a property of another server control</font></h3>
<form runat="server">
<asp:DropDownList id="StateList" runat="server">
<asp:ListItem>CA</asp:ListItem>
<asp:ListItem>IN</asp:ListItem>
<asp:ListItem>KS</asp:ListItem>
<asp:ListItem>MD</asp:ListItem>
<asp:ListItem>MI</asp:ListItem>
<asp:ListItem>OR</asp:ListItem>
<asp:ListItem>TN</asp:ListItem>
<asp:ListItem>UT</asp:ListItem>
</asp:DropDownList>
<asp:button Text="Submit" OnClick="SubmitBtn_Click" runat="server"/>
<p>
Selected State: <asp:label text='<%# StateList.SelectedItem.Text %>' runat="server"/>
</form>
</body>
</html>
[Visual Basic]
<html>
<head>
<script language="VB" runat="server">
Sub SubmitBtn_Click(sender As Object, e As EventArgs)
' Rather than explictly pull out the variable from the StateList
' and then manipulate a label control, just call Page.DataBind.
' This will evaluate any <%# %> expressions within the page.
Page.DataBind()
End Sub
</script>
</head>
<body>
<h3><font face="Verdana">Data binding to a property of another server control</font></h3>
<form runat="server">
<asp:DropDownList id="StateList" runat="server">
<asp:ListItem>CA</asp:ListItem>
<asp:ListItem>IN</asp:ListItem>
<asp:ListItem>KS</asp:ListItem>
<asp:ListItem>MD</asp:ListItem>
<asp:ListItem>MI</asp:ListItem>
<asp:ListItem>OR</asp:ListItem>
<asp:ListItem>TN</asp:ListItem>
<asp:ListItem>UT</asp:ListItem>
</asp:DropDownList>
<asp:button Text="Submit" OnClick="SubmitBtn_Click" runat="server"/>
<p>
Selected State: <asp:label text='<%# StateList.SelectedItem.Text %>' runat="server"/>
</form>
</body>
</html>