單個(gè)屬性 | Customer: <%# custID %> |
集合 Orders | <asp:ListBox id="List1" datasource='<%# myArray %>' runat="server"> |
表達(dá)式 Contact | <%# ( customer.FirstName + " " + customer.LastName ) %> |
方法的返回值 | Outstanding Balance: <%# GetBalance(custID) %> |
盡管上面的語(yǔ)法與ASP的Response.Write便捷語(yǔ)法(<%= %>)看起來(lái)類(lèi)似,但是它們的行為卻決然不同。ASP Response.Write便捷語(yǔ)法在頁(yè)面處理的時(shí)候計(jì)算值,而ASP.NET數(shù)據(jù)綁定語(yǔ)法只在DataBind方法被調(diào)用的時(shí)候才計(jì)算值。
DataBind是頁(yè)面和所有務(wù)器控件的一個(gè)方法。當(dāng)你調(diào)用父控件的DataBind的時(shí)候,它會(huì)依次調(diào)用所有子控件的DataBind方法。例如,DataList1.DataBind()就會(huì)調(diào)用DataList模板中的所有控件的DataBind方法。調(diào)用頁(yè)面的DataBind方法--Page.DataBind() 或簡(jiǎn)單地調(diào)用 DataBind()--會(huì)引發(fā)頁(yè)面上所有的數(shù)據(jù)綁定表達(dá)式的計(jì)算操作。通常只在頁(yè)面的Page_Load事件中調(diào)用DataBind方法,如下面的例子所示。
在.aspx頁(yè)面的任何宣告式片斷中,你都可以使用綁定語(yǔ)法,并為它的估值指定運(yùn)行時(shí)所期望的數(shù)據(jù)類(lèi)型。上面例子中的簡(jiǎn)單屬性、表達(dá)式和方法在被計(jì)算的時(shí)候會(huì)向用戶(hù)顯示文本內(nèi)容。在這種情況下,數(shù)據(jù)綁定表達(dá)式的值是String類(lèi)型的。在上面的集合例子中,數(shù)據(jù)綁定語(yǔ)法的值的類(lèi)型是ListBox的DataSource屬性。你會(huì)發(fā)現(xiàn)在綁定表達(dá)式中強(qiáng)制轉(zhuǎn)換值的類(lèi)型對(duì)于生成期望的結(jié)果是必要的。例如,如果count是一個(gè)整數(shù):
Number of Records: <%# count.ToString() %>
ASP.NET數(shù)據(jù)綁定語(yǔ)法支持公共變量、頁(yè)面的屬性和頁(yè)面中其它控件的屬性的綁定。下面的例子演示了如何綁定到公共變量和頁(yè)面的簡(jiǎn)單屬性。請(qǐng)注意,在DataBind()被調(diào)用之前,這些值都已經(jīng)初始化過(guò)了。
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Page.DataBind
End Sub
ReadOnly 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>
下面的例子演示如何綁定到另一個(gè)控件的屬性:
<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"/>
列表類(lèi)型的服務(wù)器控件(例如DropDownList、ListBox和HTMLSelect)把集合作為數(shù)據(jù)源。下面的例子演示如何綁定到通用語(yǔ)言運(yùn)行時(shí)集合類(lèi)型。這些控件只能綁定到支持Ienumerable、Icollection或IlistSource接口的集合。更為常見(jiàn)的是,它可以綁定到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。請(qǐng)注意DataView類(lèi)是在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)))
'填充一些數(shù)據(jù)
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
'把數(shù)據(jù)行添加到表
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
通常情況下,你可能希望綁定到頁(yè)面或控件之前先處理數(shù)據(jù)。下面的例子演示了如何綁定到表達(dá)式和方法的返回值。
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頁(yè)面框架組件提供了一個(gè)靜態(tài)的方法,它估算延遲綁定(late-bound)的數(shù)據(jù)綁定表達(dá)式并可以選擇把其結(jié)果格式化為字符串。在這種情況下,DataBinder.Eval很方便,因?yàn)樗碎_(kāi)發(fā)者把估值轉(zhuǎn)會(huì)為期望的數(shù)據(jù)類(lèi)型所必須執(zhí)行的很多顯式轉(zhuǎn)化工作。當(dāng)模板化列表中有數(shù)據(jù)綁定控件的時(shí)候,它特別有用處,因?yàn)樵谀欠N情況下,通常數(shù)據(jù)行和數(shù)據(jù)字段都必須轉(zhuǎn)換。
看看下面的例子,它需要把整數(shù)顯示為貨幣字符串。在標(biāo)準(zhǔn)的ASP.NET數(shù)據(jù)綁定語(yǔ)法中,你必須首先轉(zhuǎn)換數(shù)據(jù)行的類(lèi)型以檢索數(shù)據(jù)字段IntegerValue。接著把它作為參數(shù)傳遞給String.Format方法。
<%# String.Format("{0:c}", (CType(Container.DataItem, DataRowView)("IntegerValue"))) %>
這個(gè)語(yǔ)法很復(fù)雜并且不容易記住。與此形成對(duì)照的是,DataBinder.Eval是一個(gè)簡(jiǎn)單的方法,它只有三個(gè)參數(shù):數(shù)據(jù)項(xiàng)的命名容器(naming container)、數(shù)據(jù)字段名稱(chēng)和格式化字符串。在模板化的控件(例如FormView、 GridView、DetailsView、DataList或Repeater)中,命名容器都是Container.DataItem。頁(yè)面(Page)是另一種命名容器,也可以用于DataBinder.Eval。前面我們提到,ASP.NET 2.0為DataBinder.Eval提供了一個(gè)新的簡(jiǎn)化的語(yǔ)法(Eval),你可以在數(shù)據(jù)綁定的控件模板中使用它來(lái)自動(dòng)解析Container.DataItem。
<%# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:c}") %>
<%# Eval("IntegerValue", "{0:c}") %>
格式化字符串參數(shù)是可選的。如果省略了這個(gè)參數(shù),DataBinder.Eval會(huì)返回Object類(lèi)型值,如下所示:
<%# CType(DataBinder.Eval(Container.DataItem, "BoolValue"), Boolean) %>
我們要重點(diǎn)注意的是,與標(biāo)準(zhǔn)的數(shù)據(jù)綁定語(yǔ)法相比,DataBinder.Eval會(huì)明顯地影響性能,這是因?yàn)樗褂昧搜舆t綁定的反射(reflection)。請(qǐng)明智地使用DataBinder.Eval,特別是在不需要格式化字符串的情況下。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注