我們可以 通過控制 HeaderStyle , RowStyle , AlternatingRowStyle 和其他一些屬性來 改變 GridView, DetailsView, 和 FormView 的樣式 , 比如 cssClass, Font, BorderWidth , BorderStyle , BorderColor , Width , Height 等
?
一般 , 自定義格式化與我們所要顯示的數(shù)據(jù)的值有關(guān)系。例如 , 為了吸引用戶注意那些庫(kù)存為空的產(chǎn)品,我們可以將庫(kù)存對(duì)應(yīng)的字段 UnitsInStock ? 和 UnitsOnOrder 為 0 的數(shù)據(jù)背景色設(shè)為黃色 . 為了高亮化那些貴的產(chǎn)品,則將 UnitsInStock ? 高于 $75.00 的數(shù)據(jù)字體設(shè)置為粗體
?
?GridView, DetailsView, FormView 的格式自定義可以有多種方法 , 在本文中我們將用 DataBound 和 RowDataBound 兩種事件來完成, 在下一篇里我們將嘗試用 alternative 的方式 在 GridView 控件中使用 TemplateField ?
?
使用 DetailsView 控件的 DataBound 事件
當(dāng)綁定數(shù)據(jù)到 DetailsView 控件 , 不管是從數(shù)據(jù)控件或編碼填充數(shù)據(jù)到 DataSource 屬性并調(diào)用其 DataBind() 方法。以下幾種事件將觸發(fā)
?
- DataBinding 事件觸發(fā)
- 數(shù)據(jù)綁定到數(shù)據(jù)綁定控件
- DataBound 事件觸發(fā)
?
一般在 1,2,3 之后數(shù)據(jù)將會(huì)通過事件立即填充數(shù)據(jù)控件,我們還可以自定義事件處理來確定數(shù)據(jù)是否已經(jīng)被填充到控件中并按照我們的需要調(diào)整顯示格式。我們可以來做個(gè)例子 . 我們將創(chuàng)建一個(gè) DetailsView 來列出一個(gè)產(chǎn)品的一般信息,并且當(dāng) UnitPrice 超過 $75.00 時(shí)用粗體, italic 字體來顯示 UnitPrice 的值
?
Step 1: 在 DetailsView 中顯示產(chǎn)品信息
在 CustomFormatting 文件夾下 新建一個(gè) CustomColors.aspx 頁(yè)面 , 從工具箱中拖出一個(gè) DetailsView 控件到頁(yè)面中 , 設(shè)置 ID 為 ExpensiveProductsPriceInBoldItalic
綁定到一個(gè)新的數(shù)據(jù)源中,并配置此數(shù)據(jù)源到業(yè)務(wù)對(duì)象 ProductsBLL 類中的 GetProducts () 方法 , 這個(gè)的詳細(xì)實(shí)現(xiàn)步驟已經(jīng)在前面詳細(xì)介紹過了,這里就忽略了
?
當(dāng)您綁定
ObjectDataSource
到
DetailsView
時(shí)
,
我們可以修改一下字段列表,我選擇移除了
ProductID
,
SupplierID
,
CategoryID
,
UnitsInStock
,
UnitsOnOrder
,
ReorderLevel
和那些不被綁定的字段,他們將不會(huì)顯示在
DetailsView
列表中
,
而那些留下來的我們可以重命名他們
,
還可以修改他們的顯示格式
.
我還清空了
DetailsView
的
Height
和
Width
屬性
,
這樣當(dāng)顯示的只有一條數(shù)據(jù)時(shí)不會(huì)出現(xiàn)樣式的混亂。當(dāng)然我們面對(duì)的數(shù)據(jù)絕不只有一條這么少,顯示怎么辦呢?我們可以檢查
DetailsView
的智能感知中檢查
Enable Paging checkbox
是否被勾選上
,
這樣我們可以分頁(yè)查看所有的數(shù)據(jù)了
圖一 : 在 DetailsView 的值能感知中檢查 Enable Paging 屬性是否被勾選上
?
在經(jīng)過這些改變后, DetailsView 的代碼更改為
?
< asp : DetailsView ID ="DetailsView1" runat ="server" AllowPaging ="True" AutoGenerateRows ="False" DataKeyNames ="ProductID" DataSourceID ="ObjectDataSource1" EnableViewState ="False"> ??? < Fields > ??????? < asp : BoundField DataField ="ProductName" HeaderText ="Product" SortExpression ="ProductName" /> ??????? < asp : BoundField DataField ="CategoryName" HeaderText ="Category" ReadOnly ="True" SortExpression ="CategoryName" /> ??????? < asp : BoundField DataField ="SupplierName" HeaderText ="Supplier" ReadOnly ="True" SortExpression ="SupplierName" /> ??? ???? < asp : BoundField DataField ="QuantityPerUnit" HeaderText ="Qty/Unit" SortExpression ="QuantityPerUnit" /> ??????? < asp : BoundField DataField ="UnitPrice" DataFormatString ="{0:c}" HeaderText ="Price" ??????????? HtmlEncode ="False" SortExpression ="UnitPrice" /> ??? </ Fields > </ asp : DetailsView > |
?
您這時(shí)可以按 F5 執(zhí)行看看
? 圖二 : DetailsView 控件一次顯示一個(gè)數(shù)據(jù)
Step 2: 在 DataBound 事件中 編碼確定數(shù)據(jù)的值
為了將那些 UnitPrice 高于 $75.00 的產(chǎn)品用粗體, italic 字體顯示出來 , 我們首先需要編碼確定 UnitPrice 的值 , 對(duì)于 DetailsView 我們可以通過 DataBound 事件完成 . 我們選擇 DetailsView 并查看屬性視圖 (F4 位快捷鍵 ), 如果沒有顯示,則選擇 View (視圖)
Property Window( 屬性窗口 ), 在確保您選擇了 DetailsView 的情況下雙擊 DataBound 事件或者輸入您要?jiǎng)?chuàng)建的事件名
?

圖三 : 為 DataBound 事件創(chuàng)建一個(gè)事件處理
?
代碼中將會(huì)自動(dòng)生成以下代碼
??? protected void ExpensiveProductsPriceInBoldItalic_DataBound(object sender, EventArgs e) ??? { ? ??? } |
我們可以通過 DataItem 屬性來設(shè)置 DetailsView 的綁定項(xiàng) ( 一些強(qiáng)類型的數(shù)據(jù)行 (DataRow) 組成的強(qiáng)類型的數(shù)據(jù)表 (DataTable)), 當(dāng)數(shù)據(jù)表 (DataTable) 綁定到 DetailsView 時(shí),數(shù)據(jù)表的第一行將被自動(dòng)綁定到 DetailsView 的 DataItem 屬性 , 而 DataItem 屬性中包含有 DataRowView ( Object 類型) , 我們可以通過 DataRowView 來訪問一個(gè) ProductsRow 的 DataRow 實(shí)例 , 還可以檢測(cè) Object 的值來確定 ProductsRow 實(shí)例是否存在
?
下面的代碼描述如何確定 UnitPrice 是否綁定到 DetailsView 并且高于 $75.00
protected void ExpensiveProductsPriceInBoldItalic_DataBound(object sender, EventArgs e) { ??? // Get the ProductsRow object from the DataItem property... ??? Northwind.ProductsRow product = (Northwind.ProductsRow)((System.Data.DataRowView) ExpensiveProductsPriceInBoldItalic.DataItem).Row;
???
if (!product.IsUnitPriceNull() && product.UnitPrice > ??? { ??????? // TODO: Make the UnitPrice text bold and italic ??? } } |
?
注意 : 當(dāng) UnitPrice 在數(shù)據(jù)庫(kù)的值為空,我們?cè)诮壎ǖ?/span> ProductsRow ’s UnitPrice 屬性之前檢查確定他是否為空,這很重要因?yàn)槲覀兛梢酝ㄟ^檢查這個(gè)屬性來拋出一個(gè)強(qiáng)類型的異常 StrongTypingException exception .
?
Step 3: 在 DetailsView 中格式化 UnitPrice
到這個(gè)時(shí)候我們已經(jīng)知道即將綁定的 UnitPrice 是否高于 $75.00, 現(xiàn)在我們來看看怎么通過編碼調(diào)整 UnitPrice 的格式 , 我們可以通過修改 DetailsViewID .Rows[index] ; 修改一行數(shù)據(jù),而且我們可以通過訪問 DetailsViewID .Rows[index].Cells[index] 來訪問某一單元格 , 這樣我們可以通過修改與格式相關(guān)的屬性來格式化這一單元格
?
訪問某一行需要得到某行的索引, 索引從 0 開始 , UnitPrice 在 DetailsView 中是第 15 行 , 假設(shè)他在第四行那么我們可以通過 ExpensiveProductsPriceInBoldItalic.Rows[4] 來訪問 . 這時(shí)我們可以通過下面的代碼將這一行顯示為粗體, italic 字體
?
ExpensiveProductsPriceInBoldItalic.Rows[4].Font.Bold = true ; ExpensiveProductsPriceInBoldItalic.Rows[4].Font.Italic = true ; |
?
然而,這樣將會(huì)格式化 Label 和值,如果我們只想將值格式話,而且我們需要將格式應(yīng)用到當(dāng)前行的第二格,請(qǐng)看下面的代碼
?
ExpensiveProductsPriceInBoldItalic.Rows[4].Cells[1].Font.Bold = true; ExpensiveProductsPriceInBoldItalic.Rows[4].Cells[1].Font.Italic = true ; |
?
我們還可以通過 StyleSheet 來顯示標(biāo)記和樣式相關(guān)信息,而不是用確定的某一行某一列來設(shè)置格式,我們用 CSS 來控制格式 , 打開 Styles.css 文件,添加一個(gè)新的 Class 命名為 ExpensivePriceEmphasis 按照下面的代碼
CSS .ExpensivePriceEmphasis { ??? font-weight: bold; ??? font-style: italic; } |
?
然后再 DataBound 事件中,設(shè)置單元的 CssClass 為 ExpensivePriceEmphasis ,在 DataBound 事件處理中添加
?
當(dāng)查看 Chai( 費(fèi)用低于 $75.00), 價(jià)格將會(huì)用正常格式顯示 圖 4) ,但是當(dāng)查看 Mishi Kobe Niku,( 價(jià)格為 $97.00) 則會(huì)用我們?cè)O(shè)置的格式顯示 ( 圖 5)
圖 4: 價(jià)格低于 $75.00 將會(huì)用正常格式顯示
圖 5: 價(jià)格高于 $75.00 將會(huì)用 粗體 , Italic 字體顯示
使用 FormView 控件的 DataBound 事件
綁定到 FormView 數(shù)據(jù)的步驟和 DetailsView 的步驟類似都要?jiǎng)?chuàng)建一個(gè) DataBound 事件處理 , 聲明綁定到控件的 DataItem 類型屬性 , 然后執(zhí)行綁定。然而,他們更新的方式不同
?
FormView 不包括任何綁定列也不包含行的集合 , 取而代之的是他由一系列包含若干靜態(tài) HTML , Web 控件,綁定表達(dá)式的模板組合。調(diào)整 FormView 的外觀涉及到調(diào)整一個(gè)或多個(gè) FormView 的模板
?
讓我們像前一個(gè)例子那樣用 FormView 列出產(chǎn)品項(xiàng),但是這次我們僅僅用紅色字體顯示 units 小于等于 10 的產(chǎn)品的 name 和 units
?
Step 1: 在 FormView 中顯示產(chǎn)品信息
添加一個(gè) FormView 到 CustomColors.aspx 中,設(shè)置其 ID 為 LowStockedProductsInRed , 像前一個(gè)步驟一樣綁定數(shù)據(jù)到 ObjectDataSource 中, 這將在 FormView 中創(chuàng)建 ItemTemplate , EditItemTemplate , 和 InsertItemTemplate ?. 移除 EditItemTemplate 和 InsertItemTemplate 并在 ItemTemplate 中僅包含 ProductName 和 UnitsInStock 項(xiàng) , 在智能感知中檢查 Allow Paging (分頁(yè))標(biāo)記是否被選上
?
在這些操作后 FormView 的代碼大概會(huì)成這樣
< asp : FormView ID ="LowStockedProductsInRed" runat ="server" DataKeyNames ="ProductID" ??? DataSourceID ="ObjectDataSource1" AllowPaging ="True" EnableViewState ="False"> ??????????? ??? < ItemTemplate > ??????? < b >Product:</b> ??????? < asp : Label ID ="ProductNameLabel" runat ="server" Text =' <%# Bind("ProductName") %>'> ??????? </ asp : Label >< br /> ??????? < b >Units In Stock:</b> ??????? < asp : Label ID ="UnitsInStockLabel" runat ="server" Text =' <%# Bind("UnitsInStock") %>'> ??????? </ asp : Label > ??? </ ItemTemplate > </ asp : FormView > |
?
注意 ItemTemplate 包含的代碼 :
·???????? 靜態(tài) HTML – “Product:” 和 “Units In Stock:” 包含 <br /> 和 <b> 元素 .
·???????? Web 控件 – 兩個(gè) Label 控件 , ProductNameLabel 和 UnitsInStockLabel .
·???????? 綁定表達(dá)式 – <%# Bind("ProductName") %> 和 <%# Bind("UnitsInStock") %> 表達(dá)式 , 綁定值到 Label 的 Text 屬性上
?
?
?
Step 2: 在 DataBound 事件處理中編碼確定數(shù)據(jù)的值
當(dāng) FormView 的標(biāo)記完成后,下一步就是確定 UnitsInStock 的值是否小于等于 10, 這里和在 DetailView 中類似,先創(chuàng)建 DataBound 事件

圖 6: 創(chuàng)建 DataBound 事件處理
在事件中聲明 FormView 的 DataItem 屬性到 ProductsRow 實(shí)例中 , 確定 UnitsInPrice 的值并將對(duì)應(yīng)的值用紅色字體顯示
protected void LowStockedProductsInRed_DataBound(object sender, EventArgs e) { ??? // Get the ProductsRow object from the DataItem property... ??? Northwind.ProductsRow product = (Northwind.ProductsRow)((System.Data.DataRowView)LowStockedProductsInRed.DataItem).Row; ??? if (!product.IsUnitsInStockNull() && product.UnitsInStock <= 10) ??? { ??????? // TODO: Make the UnitsInStockLabel ’s text red ??? } } |
?
?
Step 3: 在 FormView 的 ItemTemplate 中格式化 UnitsInStockLabel Label
最后一步就是要在 ItemTemplate 中 設(shè)置 UnitsInStockLabel 的樣式為紅色字體,在 ItemTempelete 中查找控件可以使用 FindControl(“controlID”) 方法
?
WebControlType someName = (WebControlType)FormViewID.FindControl("controlID"); |
?
對(duì)于我們這個(gè)例子我們可以用如下代碼來查找該 Label 控件
?
Label unitsInStock = (Label)LowStockedProductsInRed.FindControl("UnitsInStockLabel"); |
?
當(dāng)我們找到這個(gè)控件時(shí)則可以修改其對(duì)應(yīng)的 style 屬性,在 style.css 中已經(jīng)有一個(gè)寫好的 LowUnitsInStockEmphasis 的 cSS Class , 我們通過下面的代碼將 cSS Class 設(shè)置到對(duì)應(yīng)的屬性
?
??? protected void LowStockedProductsInRed_DataBound(object sender, EventArgs e) ??? { ??????? // Get the ProductsRow object from the DataItem property... ??????? Northwind.ProductsRow product = (Northwind.ProductsRow)((System.Data.DataRowView)LowStockedProductsInRed.DataItem).Row; ??????? if (!product.IsUnitsInStockNull() && product.UnitsInStock <= 10) ??????? { ??????????? Label unitsInStock = (Label)LowStockedProductsInRed.FindControl("UnitsInStockLabel"); ? ??????????? if (unitsInStock != null) ??????????? { ??????????????? unitsInStock.CssClass = "LowUnitsInStockEmphasis"; ??????????? } ??????? } ??? } |
注意 : 這種方式在 FormView 和 GridView 中也可以通過設(shè)置 TemplateFields 來達(dá)到同樣的效果,我們將在下一篇中討論 TemplateFields
圖 7 顯示 FormView 在當(dāng) UnitsInStock 大于 10 的情況,圖 8 則顯示小于等于 10 的情況
圖 7 : 在高于 10 的情況下,沒有值被格式化

圖 8 :小于等于 10 時(shí),值用紅色字體顯示
?
?
用 GridView 的 RowDataBound 事件自定義格式化
前面我們討論了在 FormView 和 DetailsView 中實(shí)現(xiàn)數(shù)據(jù)綁定的步驟,現(xiàn)在讓我們回顧下
- DataBinding 事件觸發(fā)
- 數(shù)據(jù)綁定到數(shù)據(jù)綁定控件
- DataBound 事件觸發(fā)
對(duì)于 FormView 和 DetailsView 有效因?yàn)橹恍枰@示一個(gè)數(shù)據(jù),而在 GridView 中,則要顯示所有數(shù)據(jù),相對(duì)于前面三個(gè)步驟,步驟二有些不同
在步驟二中, GridView 列出所有的數(shù)據(jù),對(duì)于某一個(gè)記錄將創(chuàng)建一個(gè) GridViewRow 實(shí)例并綁定,對(duì)于每個(gè)添加到 GridView 中的 GridViewRow 兩個(gè)事件將會(huì)觸發(fā) :
·???????? RowCreated – 當(dāng) GridViewRow 被創(chuàng)建時(shí)觸發(fā)
·???????? RowDataBound – 當(dāng)前記錄綁定到 GridViewRow 時(shí)觸發(fā) .
?
對(duì)于 GridView ,請(qǐng)使用下面的步驟
- DataBinding 事件觸發(fā)
- 數(shù)據(jù)綁定到數(shù)據(jù)綁定控件
對(duì)于每一行數(shù)據(jù) ..
a.?????? 創(chuàng)建 GridViewRow
b.?????? 觸發(fā) RowCreated 事件
c.?????? 綁定數(shù)據(jù)到 GridViewRow
d.?????? 觸發(fā) RowDataBound 事件
e.?????? 添加 GridViewRow 到 Rows 集合
- DataBound 事件觸發(fā)
?
為了自定義格式化 GridView 單獨(dú)記錄,我們需要為 RowDataBound 事件創(chuàng)建事件處理,讓我們添加一個(gè) GridView 到 CustomColors.aspx 中,并顯示 name, category, 和 price ,用 黃色背景 高亮那些價(jià)格小于 $10.00 的產(chǎn)品
?
Step 1: 在 GridView 中顯示產(chǎn)品信息
添加一個(gè) GridView 到 FormView 的下方,設(shè)置 ID 為 HighlightCheapProducts . 我們之前已經(jīng)設(shè)置了一個(gè) ObjectDataSource 來獲取產(chǎn)品數(shù)據(jù),現(xiàn)在我們綁定 GridView 到 ObjectDataSource . 之后,編輯 GridView 的綁定列包含產(chǎn)品的 name.categorie,price 屬性。完成之后 GridView 的代碼將會(huì)是 :
< asp : GridView ID ="HighlightCheapProducts" runat ="server" AutoGenerateColumns ="False" ??? DataKeyNames ="ProductID" DataSourceID ="ObjectDataSource1" EnableViewState ="False"> ??? < Columns > ??????? < asp : BoundField DataField ="ProductName" HeaderText ="Product" SortExpression ="ProductName" /> ??????? < asp : BoundField DataField ="CategoryName" HeaderText ="Category" ReadOnly ="True" SortExpression ="CategoryName" /> ??????? < asp : BoundField DataField ="UnitPrice" DataFormatString ="{0:c}" HeaderText ="Price" ??????????? HtmlEncode ="False" SortExpression ="UnitPrice" /> ??? </ Columns > </ asp : GridView > |
?
?
?
?
圖九顯示瀏覽器查看的結(jié)果
圖 9: GridView 顯示產(chǎn)品的 name, category, price
?
Step 2: 在 RowDataBound 的 事件處理中編碼確定數(shù)據(jù)對(duì)應(yīng)的值
當(dāng) ProductsDataTable 綁定到 GridView , GridView 將會(huì)產(chǎn)生若干個(gè) ProductsRow 。 GridViewRow 的 DataItem 屬性將會(huì)生成一個(gè)實(shí)際的 ProductRow 。在 GridView 的 RowDataBound 事件發(fā)生之后,為了確定 UnitsInStock 的值,我們需要?jiǎng)?chuàng)建 RowDataBound 的事件處理,在其中我們可以確定 UnitsInStock 的值并做相應(yīng)的格式化
EventHandler 的創(chuàng)建過程和前面兩個(gè)一樣

圖 10 : 創(chuàng)建 GridView 的 RowDataBound 事件的事件處理
在后臺(tái)代碼里將會(huì)自動(dòng)生成如下代碼
protected void HighlightCheapProducts_RowDataBound(object sender, GridViewRowEventArgs e) { } |
?
當(dāng) RowDataBound 事件觸發(fā) , 第二個(gè)參數(shù) GridViewRowEventArgs 中包含了對(duì) GridViewRow 的引用,我們用如下的代碼來訪問 GridViewRow 中的 ProductsRow
??? protected void HighlightCheapProducts_RowDataBound(object sender, GridViewRowEventArgs e) ??? {??????? // Get the ProductsRow object from the DataItem property... ??????? Northwind.ProductsRow product = (Northwind.ProductsRow)((System.Data.DataRowView)e.Row.DataItem).Row;
???????
if (!product.IsUnitPriceNull() && product.UnitPrice < ??????? { ??????????? // TODO: Highlight the row yellow... ???????} ??? } |
當(dāng)運(yùn)用 RowDataBound 事件處理時(shí), GridView 由各種類型不同的行組成,而事件發(fā)生針對(duì)所有的行類型 , GridViewRow 的類型可以由 RowType 屬性決定,可以是以下類型中的一種
·???????? DataRow – GridView 的 DataSource 中的一條記錄
·???????? EmptyDataRow – GridView 的 DataSource 顯示出來的某一行為空
·???????? Footer – 底部行; 顯示由 GridView 的 ShowFooter 屬性決定
·???????? Header – 頭部行 ; 顯示由 GridView 的 ShowHeader 屬性決定
·???????? Pager – GridView 的分頁(yè),這一行顯示分頁(yè)的標(biāo)記
·???????? Separator – 對(duì)于 GridView 不可用,但是對(duì)于 DataList 和 Reapter 的 RowType 屬性卻很有用,我們將在將來的文章中討論他們
當(dāng)上面四種 (DataRow,?Pager Rows?Footer,?Header) 都不合適對(duì)應(yīng)值時(shí),將返回一個(gè)空的數(shù)據(jù)項(xiàng) , 所以我們需要在代碼中檢查 GridViewRow 的 RowType 屬性來確定 :
?
protected void HighlightCheapProducts_RowDataBound(object sender, GridViewRowEventArgs e) { ??????? // Make sure we are working with a DataRow ??????? if (e.Row.RowType == DataControlRowType.DataRow) ??????? { ??????????? // Get the ProductsRow object from the DataItem property... ??????????? Northwind.ProductsRow product = (Northwind.ProductsRow)((System.Data.DataRowView)e.Row.DataItem).Row;
???????????
if (!product.IsUnitPriceNull() && product.UnitPrice < ??????????? { ??????????????? // TODO: Highlight row yellow... ??????? ????} ??????? } } |
?
Step 3: 用黃色高亮那些 UnitPrice 小于 $10.00 的行
我們需要訪問 GridViewID .Rows[index] 來訪問 index 對(duì)應(yīng)的那一行, GridViewID .Rows[index].Cells[index] 來訪問某一單元格 . 然而當(dāng) RowDataBound 事件觸發(fā)時(shí), GridViewRow 卻沒有添加到 Rows 集合中 , 因此我們不能在 RowDataBound 事件處理中通過當(dāng)前 GridViewRow 實(shí)例
?
取而代之,我們可以通過 e.Row 來訪問。為了高亮某一行我們用下面的代碼
e.Row.BackColor = System.Drawing. Color .Yellow; |
我們還可以通過 cSSClass 取得同樣的效果 ( 推薦 )
??? protected void HighlightCheapProducts_RowDataBound(object sender, GridViewRowEventArgs e) ??? { ??????? // Make sure we are working with a DataRow ??????? if (e.Row.RowType == DataControlRowType.DataRow) ??????? { ??????????? // Get the ProductsRow object from the DataItem property... ??????????? Northwind.ProductsRow product = (Northwind.ProductsRow)((System.Data.DataRowView)e.Row.DataItem).Row;
???????????
if (!product.IsUnitPriceNull() && product.UnitPrice < ??????????? { ??????????????? e.Row.CssClass = "AffordablePriceEmphasis"; ??????????? } ??????? } ??? } |
圖 11: 所需要的行用高亮黃色顯示
?
?
?
總結(jié)
在本篇中我們演示了基于數(shù)據(jù)綁定來自定義格式化 GridView,?DetailsView,?FormView 的方法。為了完成這些,我們創(chuàng)建 DataBound 或者 RowDataBound 事件 , 為了訪問 DetailsView 或 FormView 的數(shù)據(jù)綁定,我們可以通過 DataItem 屬性。對(duì)于 GridView ,每個(gè) GridViewRow 實(shí)例的 DataItem 屬性包含了綁定的數(shù)據(jù) ( 在 RowDataBound 事件處理中可用 )
?
為了調(diào)整格式,我們可能需要訪問某一特定的行,在 GridView 和 DetailsView 中我們可以通過索引訪問,而在 FormView 中我們則需要用 FindControl("controlID") , 同時(shí) FindControl("controlID") 通常都可以訪問 Web 控件 Tempelete 中的某個(gè)控件
?
在下一篇中我們將討論如何在 GridView 和 DetailsView 使用 Tempeletes, 還將討論另外一些自定義格式化的方法