數(shù)據(jù)綁定概要
<%# %> 語法 :
ASP.NET 引入了一種新的聲明語法 <%# %>,。該語法是在 .aspx 頁中使用數(shù)據(jù)綁定的基礎(chǔ),。所有數(shù)據(jù)綁定表達式都必須包含在這些字符中,。
簡單屬性(用于客戶的語法):
<%# custID %>
集合(用于訂單的語法):
<asp:ListBox id="List1" datasource='<%# myArray %>' runat="server">
表達式(用于聯(lián)系人的語法):
<%# ( customer.First Name + " " + customer.LastName ) %>
方法結(jié)果(用于未結(jié)清余額的語法):
<%# GetBalance(custID) %>
Page.DataBind() 與 Control.DataBind()為 .aspx 頁上的對象確定并設(shè)置了特定數(shù)據(jù)源后,,必須將數(shù)據(jù)綁定到這些數(shù)據(jù)源,。您可以使用 Page.DataBind 或Control.DataBind 方法將數(shù)據(jù)綁定到數(shù)據(jù)源,。
這兩種方法的使用方式很相似,。主要差別在于:調(diào)用 Page.DataBind 方法后,所有數(shù)據(jù)源都將綁定到它們的服務(wù)器控件,。在顯式調(diào)用 Web 服務(wù)器控件的 DataBind 方法或在調(diào)用頁面級的 Page.DataBind 方法之前,,不會有任何數(shù)據(jù)呈現(xiàn)給控件。通常,,可以從 Page_Load 事件調(diào)用 Page.DataBind(或 DataBind),。
數(shù)據(jù)綁定列表控件
列表控件是可以綁定到集合的特殊的 Web 服務(wù)器控件。您可以使用這些控件以自定義的模板格式顯示數(shù)據(jù)行,。所有列表控件都公開 DataSource 和 DataMember 屬性,,這些屬性用于綁定到集合。
這些控件可以將其 DataSource 屬性綁定到支持 IEnumerable、ICollection 或 IListSource 接口的任一集合,。
Repeater.DataSource 屬性:獲取或設(shè)置為填充列表提供數(shù)據(jù)的數(shù)據(jù)源,。
Repeater 控件
Repeater 控件是模板化的數(shù)據(jù)綁定列表。Repeater 控件是“無外觀的”,;即,,它不具有任何內(nèi)置布局或樣式。因此,,您必須在控件的模板中明確聲明所有 HTML 布局標記,、格式標記和樣式標記。
以下代碼示例向您演示了如何使用 Repeater 這一列表控件顯示數(shù)據(jù):
<%@ Page language="c#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
SqlConnection cnn = new
SqlConnection("server=(local);database=pubs;Integrated Security=SSPI");
SqlDataAdapter da = new SqlDataAdapter("select * from authors", cnn);
DataSet ds = new DataSet();
da.Fill(ds, "authors");
Repeater1.DataSource = ds.Tables["authors"];
Repeater1.DataBind();
}
</script>
<html>
<body>
<form id="WebForm2" method="post" runat="server">
<asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"au_id") %><br>
</ItemTemplate>
</asp:Repeater>
</form>
</body>
</html>
DataBinder.Eval用法范例
<%# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:c}") %>
格式化字符串參數(shù)是可選的,。如果忽略參數(shù),,DataBinder.Eval 返回對象類型的值,
//顯示二位小數(shù)
<%# DataBinder.Eval(Container.DataItem, "UnitPrice", "${0:F2}") %>
//{0:G}代表顯示True或False
<ItemTemplate>
<asp:Image Width="12" Height="12" Border="0" runat="server"
AlternateText='<%# DataBinder.Eval(Container.DataItem, "Discontinued", "{0:G}") %>'
ImageUrl='<%# DataBinder.Eval(Container.DataItem, "Discontinued", "~/images/{0:G}.gif") %>' />
</ItemTemplate>
//轉(zhuǎn)換類型
((string)DataBinder.Eval(Container, "DataItem.P_SHIP_TIME_SBM8")).Substring(4,4)
{0:d} 日期只顯示年月日
{0:yyyy-mm-dd} 按格式顯示年月日
{0:c} 貨幣樣式
<%#Container.DataItem("price","{0:¥#,##0.00}")%>
<%# DataBinder.Eval(Container.DataItem,"Company_Ureg_Date","{0:yyyy-M-d}")%>
Specifier Type Format Output (Passed Double 1.42) Output (Passed Int -12400)
c Currency {0:c} $1.42 -$12,400
d Decimal {0:d} System.FormatException -12400
e Scientific {0:e} 1.420000e+000 -1.240000e+004
f Fixed point {0:f} 1.42 -12400.00
g General {0:g} 1.42 -12400
n Number with commas for thousands {0:n} 1.42 -12,400
r Round trippable {0:r} 1.42 System.FormatException
x Hexadecimal {0:x4} System.FormatException cf90
{0:d} 日期只顯示年月日
{0:yyyy-mm-dd} 按格式顯示年月日
樣式取決于 Web.config 中的設(shè)置
{0:c} 或 {0:£0,000.00} 貨幣樣式 標準英國貨幣樣式
<system.web>
<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="en-US" uiCulture="en-US" />
</system.web>
顯示為 £3,000.10
{0:c} 或 string.Format("{0:C}", price); 中國貨幣樣式
<system.web>
<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="zh-cn" uiCulture="zh-cn" />
</system.web>
顯示為 ¥3,000.10
{0:c} 或 string.Format("{0:C}", price); 美國貨幣樣式
<system.web>
<globalization requestEncoding="utf-8" responseEncoding="utf-8" />
</system.web>
顯示為 $3,000.10