關於ASP.NET 資料繫結

來源:互聯網
上載者:User

資料繫結概要
<%# %> 文法 :
ASP.NET 引入了一種新的聲明文法 <%# %>。該文法是在 .aspx 頁中使用資料繫結的基礎。所有資料繫結運算式都必須包含在這些字元中。
簡單屬性(用於客戶的文法):
<%# custID %>
集合(用於訂單的文法):
<asp:ListBox id="List1" datasource='<%# myArray %>' runat="server">
 運算式(用於連絡人的文法):
<%# ( customer.First Name + " " + customer.LastName ) %>
 方法結果(用於未結清餘額的文法):
<%# GetBalance(custID) %>
                  

  Page.DataBind() 與 Control.DataBind()為 .aspx 頁上的對象確定並設定了特定資料來源後,必須將資料繫結到這些資料來源。您可以使用 Page.DataBind 或Control.DataBind 方法將資料繫結到資料來源。
  這兩種方法的使用方式很相似。主要差別在於:調用 Page.DataBind 方法後,所有資料來源都將綁定到它們的伺服器控制項。在顯式調用 Web 伺服器控制項的 DataBind 方法或在調用頁面級的 Page.DataBind 方法之前,不會有任何資料呈現給控制項。通常,可以從 Page_Load 事件調用 Page.DataBind(或 DataBind)。


資料繫結清單控制項
  清單控制項是可以綁定到集合的特殊的 Web 伺服器控制項。您可以使用這些控制項以自訂的模板格式顯示資料行。所有清單控制項都公開 DataSource 和 DataMember 屬性,這些屬性用於綁定到集合。
  這些控制項可以將其 DataSource 屬性綁定到支援 IEnumerable、ICollection 或 IListSource 介面的任一集合。
Repeater.DataSource 屬性:擷取或設定為填充列表提供資料的資料來源。

重複器控制項
  重複器控制項是模板化的資料繫結列表。重複器控制項是“無外觀的”;即,它不具有任何內建布局或樣式。因此,您必須在控制項的模板中明確聲明所有 HTML 布局標記、格式標記和樣式標記。
以下程式碼範例向您示範了如何使用 Repeater 這一清單控制項顯示資料:

<%@ 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}") %>
格式化字串參數是可選的。如果忽略參數,DataBinder.Eval 返回物件類型的值,

//顯示二位小數
<%# 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>

//轉換類型
((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 中的設定
{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

                                       參考:http://support.microsoft.com/kb/307860/zh-cn
                                       鳴謝:熠哥

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.