在前面的"模板中的資料繫結"部分中我們論述過,ASP.NET包含了一種宣告式的資料繫結文法,用於在資料繫結模板中把資料來源欄位與控制項的屬性相關聯。你也可以在代碼中使用<%# ... >文法來進行任意值的資料繫結,例如頁面和控制項屬性、集合、運算式,甚至於方法調用的返回結果。為了強制計算資料繫結的值,你必須在包含資料繫結文法的頁面或控制項上調用DataBind方法。下面的表格顯示了ASP.NET中的資料繫結文法的一些例子。
| 單個屬性 |
Customer: <%# custID %> |
| 集合 Orders |
<asp:ListBox id="List1" datasource='<%# myArray %>' runat="server"> |
| 運算式 Contact |
<%# ( customer.FirstName + " " + customer.LastName ) %> |
| 方法的傳回值 |
Outstanding Balance: <%# GetBalance(custID) %> |
儘管上面的文法與ASP的Response.Write便捷文法(<%= %>)看起來類似,但是它們的行為卻決然不同。ASP Response.Write便捷文法在頁面處理的時候計算值,而ASP.NET資料繫結文法只在DataBind方法被調用的時候才計算值。
DataBind是頁面和所有務器控制項的一個方法。當你調用父控制項的DataBind的時候,它會依次調用所有子控制項的DataBind方法。例如,DataList1.DataBind()就會調用DataList模板中的所有控制項的DataBind方法。調用頁面的DataBind方法--Page.DataBind() 或簡單地調用 DataBind()--會引發頁面上所有的資料繫結運算式的計算操作。通常只在頁面的Page_Load事件中調用DataBind方法,如下面的例子所示。
在.aspx頁面的任何宣告式片斷中,你都可以使用綁定文法,並為它的估值指定運行時所期望的資料類型。上面例子中的簡單屬性、運算式和方法在被計算的時候會向使用者顯示常值內容。在這種情況下,資料繫結運算式的值是String類型的。在上面的集合例子中,資料繫結文法的值的類型是ListBox的DataSource屬性。你會發現在綁定運算式中強制轉換值的類型對於產生期望的結果是必要的。例如,如果count是一個整數:
| Number of Records: <%# count.ToString() %> |
ASP.NET資料繫結文法支援公開變數、頁面的屬性和頁面中其它控制項的屬性的綁定。下面的例子示範了如何綁定到公開變數和頁面的簡單屬性。請注意,在DataBind()被調用之前,這些值都已經初始化過了。
<script language="VB" runat="server"> Sub Page_Load(sender As Object, e As EventArgs) Page.DataBind End SubReadOnly Property custID() As String Get Return "ALFKI" End Get End Property ReadOnly Property orderCount() As Integer Get Return 11 End Get End Property </script> <form action="DataBind1_vb.aspx" runat="server"> Customer: <b><%# custID %></b><br /> Open Orders: <b><%# orderCount %></b> </form> |
下面的例子示範如何綁定到另一個控制項的屬性:
<asp:DropDownList id="StateList" runat="server"> <asp:ListItem>CA</asp:ListItem> …… </asp:DropDownList><asp:button ID="Button1" Text="Submit" OnClick="SubmitBtn_Click" runat="server"/> Selected State: <asp:label ID="Label1" text='<%# StateList.SelectedItem.Text %>' runat="server"/> |
清單類型的伺服器控制項(例如DropDownList、ListBox和HTMLSelect)把集合作為資料來源。下面的例子示範如何綁定到通用語言運行時集合類型。這些控制項只能綁定到支援Ienumerable、Icollection或IlistSource介面的集合。更為常見的是,它可以綁定到ArrayList、Hashtable、DataView和DataReader。下面的例子示範了如何綁定到ArrayList。
Sub Page_Load(sender As Object, e As EventArgs) If Not IsPostBack Then Dim values as ArrayList= new ArrayList() values.Add ("IN") values.Add ("KS") values.Add ("MD") values.Add ("MI") values.Add ("OR") values.Add ("TN") DropDown1.DataSource = values DropDown1.DataBind End If End Sub |
下面的例子示範了如何綁定到DataView。請注意DataView類是在System.Data名字空間中定義的。
Sub Page_Load(sender As Object, e As EventArgs) If Not IsPostBack Then Dim dt As DataTable Dim dr As DataRow Dim i As Integer '建立DataTable dt = New DataTable dt.Columns.Add(New DataColumn("IntegerValue", GetType(Integer))) dt.Columns.Add(New DataColumn("StringValue", GetType(String))) dt.Columns.Add(New DataColumn("DateTimeValue", GetType(DateTime))) dt.Columns.Add(New DataColumn("BooleanValue", GetType(Boolean))) '填充一些資料 For i = 1 To 9 dr = dt.NewRow() dr(0) = i dr(1) = "Item " + i.ToString() dr(2) = DateTime.Now.ToShortTimeString If (i Mod 2 <> 0) Then dr(3) = True Else dr(3) = False End If '把資料行添加到表 dt.Rows.Add(dr) Next GridView1.DataSource = New DataView(dt) GridView1.DataBind() End If End Sub |
下面的例子示範了如何綁定到Hashtable。
Sub Page_Load(sender As Object, e As EventArgs) If Not IsPostBack Then Dim h As Hashtable = new Hashtable() h.Add ("key1", "value1") h.Add ("key2", "value2") h.Add ("key3", "value3") MyDataList.DataSource = h MyDataList.DataBind End If End Sub |
通常情況下,你可能希望綁定到頁面或控制項之前先處理資料。下面的例子示範了如何綁定到運算式和方法的傳回值。
Sub Page_Load(sender As Object, e As EventArgs) If Not IsPostBack Then Dim values as ArrayList= new ArrayList() values.Add (0) values.Add (1) values.Add (2) values.Add (3) values.Add (4) values.Add (5) values.Add (6) DataList1.DataSource = values DataList1.DataBind End If End Sub Function EvenOrOdd(number As Integer) As String If (number Mod 2 <> 0) Then Return "Odd" Else Return "Even" End If End Function <asp:DataList id="DataList1" ……> <ItemTemplate> Number Value: <%# Container.DataItem %> Even/Odd: <%# EvenOrOdd(Container.DataItem) %> </ItemTemplate> </asp:DataList> |
ASP.NET頁面架構組件提供了一個靜態方法,它估算延遲綁定(late-bound)的資料繫結運算式並可以選擇把其結果格式化為字串。在這種情況下,DataBinder.Eval很方便,因為它消除了開發人員把估值轉會為期望的資料類型所必須執行的很多顯式轉化工作。當模板化列表中有資料繫結控制項的時候,它特別有用處,因為在那種情況下,通常資料行和資料欄位都必須轉換。
看看下面的例子,它需要把整數顯示為貨幣字串。在標準的ASP.NET資料繫結文法中,你必須首先轉換資料行的類型以檢索資料欄位IntegerValue。接著把它作為參數傳遞給String.Format方法。
| <%# String.Format("{0:c}", (CType(Container.DataItem, DataRowView)("IntegerValue"))) %> |
這個文法很複雜並且不容易記住。與此形成對照的是,DataBinder.Eval是一個簡單的方法,它只有三個參數:資料項目的命名容器(naming container)、資料欄位名稱和格式化字串。在模板化的控制項(例如FormView、 GridView、DetailsView、DataList或Repeater)中,命名容器都是Container.DataItem。頁面(Page)是另一種命名容器,也可以用於DataBinder.Eval。前面我們提到,ASP.NET 2.0為DataBinder.Eval提供了一個新的簡化的文法(Eval),你可以在資料繫結的控制項範本中使用它來自動解析Container.DataItem。
<%# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:c}") %> <%# Eval("IntegerValue", "{0:c}") %> |
格式化字串參數是可選的。如果省略了這個參數,DataBinder.Eval會返回Object類型值,如下所示:
| <%# CType(DataBinder.Eval(Container.DataItem, "BoolValue"), Boolean) %> |
我們要重點注意的是,與標準的資料繫結文法相比,DataBinder.Eval會明顯地影響效能,這是因為它使用了延遲綁定的反射(reflection)。請明智地使用DataBinder.Eval,特別是在不需要格式化字串的情況下。
本文轉載於‘中國IT實驗室’