色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

ASP.NET 2.0數(shù)據(jù)教程之十一:基于數(shù)據(jù)的自定義格式化

系列文章導(dǎo)航:

ASP.NET 2.0數(shù)據(jù)教程之一:創(chuàng)建一個(gè)數(shù)據(jù)訪問層

ASP.NET 2.0數(shù)據(jù)教程之二:創(chuàng)建一個(gè)業(yè)務(wù)邏輯層

ASP.NET 2.0數(shù)據(jù)教程之三:母板頁和站點(diǎn)導(dǎo)航

ASP.NET 2.0數(shù)據(jù)教程之四:使用ObjectDataSource展現(xiàn)數(shù)據(jù)

ASP.NET 2.0數(shù)據(jù)教程之五:聲明參數(shù)

ASP.NET 2.0數(shù)據(jù)教程之六:編程設(shè)置ObjectDataSource的參數(shù)值

ASP.NET 2.0數(shù)據(jù)教程之七:使用DropDownList過濾的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之八:使用兩個(gè)DropDownList過濾的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之九:跨頁面的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之十:使用 GridView 和DetailView實(shí)現(xiàn)的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之十一:基于數(shù)據(jù)的自定義格式化

ASP.NET 2.0數(shù)據(jù)教程之十二:在GridView控件中使用TemplateField


介紹

我們可以通過控制HeaderStyle, RowStyle, AlternatingRowStyle和其他一些屬性來改變GridView, DetailsView, FormView的樣式,比如cssClass, Font, BorderWidth, BorderStyle, BorderColor, Width, Height

一般,自定義格式化與我們所要顯示的數(shù)據(jù)的值有關(guān)系。例如, 為了吸引用戶注意那些庫存為空的產(chǎn)品,我們可以將庫存對(duì)應(yīng)的字段UnitsInStock  UnitsOnOrder0的數(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ā)

  1. DataBinding事件觸發(fā)
  2. 數(shù)據(jù)綁定到數(shù)據(jù)綁定控件
  3. 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的值

系列文章導(dǎo)航:

ASP.NET 2.0數(shù)據(jù)教程之一:創(chuàng)建一個(gè)數(shù)據(jù)訪問層

ASP.NET 2.0數(shù)據(jù)教程之二:創(chuàng)建一個(gè)業(yè)務(wù)邏輯層

ASP.NET 2.0數(shù)據(jù)教程之三:母板頁和站點(diǎn)導(dǎo)航

ASP.NET 2.0數(shù)據(jù)教程之四:使用ObjectDataSource展現(xiàn)數(shù)據(jù)

ASP.NET 2.0數(shù)據(jù)教程之五:聲明參數(shù)

ASP.NET 2.0數(shù)據(jù)教程之六:編程設(shè)置ObjectDataSource的參數(shù)值

ASP.NET 2.0數(shù)據(jù)教程之七:使用DropDownList過濾的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之八:使用兩個(gè)DropDownList過濾的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之九:跨頁面的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之十:使用 GridView 和DetailView實(shí)現(xiàn)的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之十一:基于數(shù)據(jù)的自定義格式化

ASP.NET 2.0數(shù)據(jù)教程之十二:在GridView控件中使用TemplateField


Step 1: DetailsView中顯示產(chǎn)品信息

CustomFormatting文件夾下新建一個(gè)CustomColors.ASPx頁面,從工具箱中拖出一個(gè)DetailsView控件到頁面中,設(shè)置IDExpensiveProductsPriceInBoldItalic

綁定到一個(gè)新的數(shù)據(jù)源中,并配置此數(shù)據(jù)源到業(yè)務(wù)對(duì)象ProductsBLL類中的GetProducts()方法,這個(gè)的詳細(xì)實(shí)現(xiàn)步驟已經(jīng)在前面詳細(xì)介紹過了,這里就忽略了

當(dāng)您綁定ObjectDataSourceDetailsView時(shí),我們可以修改一下字段列表,我選擇移除了ProductID, SupplierID, CategoryID, UnitsInStock, UnitsOnOrder, ReorderLevel和那些不被綁定的字段,他們將不會(huì)顯示在DetailsView列表中,而那些留下來的我們可以重命名他們,還可以修改他們的顯示格式. 我還清空了DetailsViewHeightWidth屬性,這樣當(dāng)顯示的只有一條數(shù)據(jù)時(shí)不會(huì)出現(xiàn)樣式的混亂。當(dāng)然我們面對(duì)的數(shù)據(jù)絕不只有一條這么少,顯示怎么辦呢?我們可以檢查DetailsView的智能感知中檢查Enable Paging checkbox是否被勾選上, 這樣我們可以分頁查看所有的數(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ù)

系列文章導(dǎo)航:

ASP.NET 2.0數(shù)據(jù)教程之一:創(chuàng)建一個(gè)數(shù)據(jù)訪問層

ASP.NET 2.0數(shù)據(jù)教程之二:創(chuàng)建一個(gè)業(yè)務(wù)邏輯層

ASP.NET 2.0數(shù)據(jù)教程之三:母板頁和站點(diǎn)導(dǎo)航

ASP.NET 2.0數(shù)據(jù)教程之四:使用ObjectDataSource展現(xiàn)數(shù)據(jù)

ASP.NET 2.0數(shù)據(jù)教程之五:聲明參數(shù)

ASP.NET 2.0數(shù)據(jù)教程之六:編程設(shè)置ObjectDataSource的參數(shù)值

ASP.NET 2.0數(shù)據(jù)教程之七:使用DropDownList過濾的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之八:使用兩個(gè)DropDownList過濾的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之九:跨頁面的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之十:使用 GridView 和DetailView實(shí)現(xiàn)的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之十一:基于數(shù)據(jù)的自定義格式化

ASP.NET 2.0數(shù)據(jù)教程之十二:在GridView控件中使用TemplateField


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)綁定到DetailsViewDataItem屬性,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 > 75m)

    {

        // TODO: Make the UnitPrice text bold and italic

    }

}

 

注意: 當(dāng)UnitPrice在數(shù)據(jù)庫的值為空,我們?cè)诮壎ǖ?/span>ProductsRow’s UnitPrice屬性之前檢查確定他是否為空,這很重要因?yàn)槲覀兛梢酝ㄟ^檢查這個(gè)屬性來拋出一個(gè)強(qiáng)類型的異常 StrongTypingException exception.

系列文章導(dǎo)航:

ASP.NET 2.0數(shù)據(jù)教程之一:創(chuàng)建一個(gè)數(shù)據(jù)訪問層

ASP.NET 2.0數(shù)據(jù)教程之二:創(chuàng)建一個(gè)業(yè)務(wù)邏輯層

ASP.NET 2.0數(shù)據(jù)教程之三:母板頁和站點(diǎn)導(dǎo)航

ASP.NET 2.0數(shù)據(jù)教程之四:使用ObjectDataSource展現(xiàn)數(shù)據(jù)

ASP.NET 2.0數(shù)據(jù)教程之五:聲明參數(shù)

ASP.NET 2.0數(shù)據(jù)教程之六:編程設(shè)置ObjectDataSource的參數(shù)值

ASP.NET 2.0數(shù)據(jù)教程之七:使用DropDownList過濾的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之八:使用兩個(gè)DropDownList過濾的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之九:跨頁面的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之十:使用 GridView 和DetailView實(shí)現(xiàn)的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之十一:基于數(shù)據(jù)的自定義格式化

ASP.NET 2.0數(shù)據(jù)教程之十二:在GridView控件中使用TemplateField


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è)置單元的CssClassExpensivePriceEmphasis,在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 字體顯示

系列文章導(dǎo)航:

ASP.NET 2.0數(shù)據(jù)教程之一:創(chuàng)建一個(gè)數(shù)據(jù)訪問層

ASP.NET 2.0數(shù)據(jù)教程之二:創(chuàng)建一個(gè)業(yè)務(wù)邏輯層

ASP.NET 2.0數(shù)據(jù)教程之三:母板頁和站點(diǎn)導(dǎo)航

ASP.NET 2.0數(shù)據(jù)教程之四:使用ObjectDataSource展現(xiàn)數(shù)據(jù)

ASP.NET 2.0數(shù)據(jù)教程之五:聲明參數(shù)

ASP.NET 2.0數(shù)據(jù)教程之六:編程設(shè)置ObjectDataSource的參數(shù)值

ASP.NET 2.0數(shù)據(jù)教程之七:使用DropDownList過濾的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之八:使用兩個(gè)DropDownList過濾的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之九:跨頁面的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之十:使用 GridView 和DetailView實(shí)現(xiàn)的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之十一:基于數(shù)據(jù)的自定義格式化

ASP.NET 2.0數(shù)據(jù)教程之十二:在GridView控件中使用TemplateField


使用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)品的nameunits

Step 4: FormView中顯示產(chǎn)品信息

添加一個(gè)FormViewCustomColors.ASPx中,設(shè)置其IDLowStockedProductsInRed,像前一個(gè)步驟一樣綁定數(shù)據(jù)到ObjectDataSource中, 這將在FormView中創(chuàng)建ItemTemplate, EditItemTemplate, InsertItemTemplate . 移除EditItemTemplateInsertItemTemplate 并在 ItemTemplate 中僅包含ProductName UnitsInStock 項(xiàng), 在智能感知中檢查Allow Paging(分頁)標(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á)式, 綁定值到LabelText屬性上

系列文章導(dǎo)航:

ASP.NET 2.0數(shù)據(jù)教程之一:創(chuàng)建一個(gè)數(shù)據(jù)訪問層

ASP.NET 2.0數(shù)據(jù)教程之二:創(chuàng)建一個(gè)業(yè)務(wù)邏輯層

ASP.NET 2.0數(shù)據(jù)教程之三:母板頁和站點(diǎn)導(dǎo)航

ASP.NET 2.0數(shù)據(jù)教程之四:使用ObjectDataSource展現(xiàn)數(shù)據(jù)

ASP.NET 2.0數(shù)據(jù)教程之五:聲明參數(shù)

ASP.NET 2.0數(shù)據(jù)教程之六:編程設(shè)置ObjectDataSource的參數(shù)值

ASP.NET 2.0數(shù)據(jù)教程之七:使用DropDownList過濾的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之八:使用兩個(gè)DropDownList過濾的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之九:跨頁面的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之十:使用 GridView 和DetailView實(shí)現(xiàn)的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之十一:基于數(shù)據(jù)的自定義格式化

ASP.NET 2.0數(shù)據(jù)教程之十二:在GridView控件中使用TemplateField


Step 5: DataBound 事件處理中編碼確定數(shù)據(jù)的值

當(dāng)FormView的標(biāo)記完成后,下一步就是確定UnitsInStock的值是否小于等于10,這里和在DetailView中類似,先創(chuàng)建DataBound事件


/p>

6: 創(chuàng)建 DataBound 事件處理

在事件中聲明FormViewDataItem屬性到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 UnitsInStockLabels text red

    }

}

系列文章導(dǎo)航:

ASP.NET 2.0數(shù)據(jù)教程之一:創(chuàng)建一個(gè)數(shù)據(jù)訪問層

ASP.NET 2.0數(shù)據(jù)教程之二:創(chuàng)建一個(gè)業(yè)務(wù)邏輯層

ASP.NET 2.0數(shù)據(jù)教程之三:母板頁和站點(diǎn)導(dǎo)航

ASP.NET 2.0數(shù)據(jù)教程之四:使用ObjectDataSource展現(xiàn)數(shù)據(jù)

ASP.NET 2.0數(shù)據(jù)教程之五:聲明參數(shù)

ASP.NET 2.0數(shù)據(jù)教程之六:編程設(shè)置ObjectDataSource的參數(shù)值

ASP.NET 2.0數(shù)據(jù)教程之七:使用DropDownList過濾的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之八:使用兩個(gè)DropDownList過濾的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之九:跨頁面的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之十:使用 GridView 和DetailView實(shí)現(xiàn)的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之十一:基于數(shù)據(jù)的自定義格式化

ASP.NET 2.0數(shù)據(jù)教程之十二:在GridView控件中使用TemplateField


Step 6: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è)寫好的LowUnitsInStockEmphasiscSS 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";

            }

        }

    }

注意: 這種方式在FormViewGridView中也可以通過設(shè)置TemplateFields來達(dá)到同樣的效果,我們將在下一篇中討論TemplateFields

7顯示FormView在當(dāng)UnitsInStock大于10的情況,圖8則顯示小于等于10的情況


7 : 在高于10的情況下,沒有值被格式化

8:小于等于10時(shí),值用紅色字體顯示

系列文章導(dǎo)航:

ASP.NET 2.0數(shù)據(jù)教程之一:創(chuàng)建一個(gè)數(shù)據(jù)訪問層

ASP.NET 2.0數(shù)據(jù)教程之二:創(chuàng)建一個(gè)業(yè)務(wù)邏輯層

ASP.NET 2.0數(shù)據(jù)教程之三:母板頁和站點(diǎn)導(dǎo)航

ASP.NET 2.0數(shù)據(jù)教程之四:使用ObjectDataSource展現(xiàn)數(shù)據(jù)

ASP.NET 2.0數(shù)據(jù)教程之五:聲明參數(shù)

ASP.NET 2.0數(shù)據(jù)教程之六:編程設(shè)置ObjectDataSource的參數(shù)值

ASP.NET 2.0數(shù)據(jù)教程之七:使用DropDownList過濾的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之八:使用兩個(gè)DropDownList過濾的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之九:跨頁面的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之十:使用 GridView 和DetailView實(shí)現(xiàn)的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之十一:基于數(shù)據(jù)的自定義格式化

ASP.NET 2.0數(shù)據(jù)教程之十二:在GridView控件中使用TemplateField


GridView RowDataBound 事件自定義格式化

前面我們討論了在FormViewDetailsView中實(shí)現(xiàn)數(shù)據(jù)綁定的步驟,現(xiàn)在讓我們回顧下

  1. DataBinding事件觸發(fā)
  2. 數(shù)據(jù)綁定到數(shù)據(jù)綁定控件
  3. DataBound事件觸發(fā)

對(duì)于FormViewDetailsView有效因?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)使用下面的步驟

  1. DataBinding事件觸發(fā)
  2. 數(shù)據(jù)綁定到數(shù)據(jù)綁定控件

對(duì)于每一行數(shù)據(jù)..

a.       創(chuàng)建GridViewRow

b.       觸發(fā) RowCreated 事件

c.       綁定數(shù)據(jù)到GridViewRow

d.       觸發(fā)RowDataBound事件

e.       添加GridViewRowRows 集合

  1. DataBound事件觸發(fā)

為了自定義格式化GridView單獨(dú)記錄,我們需要為RowDataBound事件創(chuàng)建事件處理,讓我們添加一個(gè)GridViewCustomColors.ASPx中,并顯示name, category, price,用黃色背景高亮那些價(jià)格小于$10.00的產(chǎn)品

系列文章導(dǎo)航:

ASP.NET 2.0數(shù)據(jù)教程之一:創(chuàng)建一個(gè)數(shù)據(jù)訪問層

ASP.NET 2.0數(shù)據(jù)教程之二:創(chuàng)建一個(gè)業(yè)務(wù)邏輯層

ASP.NET 2.0數(shù)據(jù)教程之三:母板頁和站點(diǎn)導(dǎo)航

ASP.NET 2.0數(shù)據(jù)教程之四:使用ObjectDataSource展現(xiàn)數(shù)據(jù)

ASP.NET 2.0數(shù)據(jù)教程之五:聲明參數(shù)

ASP.NET 2.0數(shù)據(jù)教程之六:編程設(shè)置ObjectDataSource的參數(shù)值

ASP.NET 2.0數(shù)據(jù)教程之七:使用DropDownList過濾的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之八:使用兩個(gè)DropDownList過濾的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之九:跨頁面的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之十:使用 GridView 和DetailView實(shí)現(xiàn)的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之十一:基于數(shù)據(jù)的自定義格式化

ASP.NET 2.0數(shù)據(jù)教程之十二:在GridView控件中使用TemplateField


Step 7:GridView中顯示產(chǎn)品信息

添加一個(gè)GridViewFormView的下方,設(shè)置IDHighlightCheapProducts.我們之前已經(jīng)設(shè)置了一個(gè)ObjectDataSource來獲取產(chǎn)品數(shù)據(jù),現(xiàn)在我們綁定GridViewObjectDataSource. 之后,編輯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

系列文章導(dǎo)航:

ASP.NET 2.0數(shù)據(jù)教程之一:創(chuàng)建一個(gè)數(shù)據(jù)訪問層

ASP.NET 2.0數(shù)據(jù)教程之二:創(chuàng)建一個(gè)業(yè)務(wù)邏輯層

ASP.NET 2.0數(shù)據(jù)教程之三:母板頁和站點(diǎn)導(dǎo)航

ASP.NET 2.0數(shù)據(jù)教程之四:使用ObjectDataSource展現(xiàn)數(shù)據(jù)

ASP.NET 2.0數(shù)據(jù)教程之五:聲明參數(shù)

ASP.NET 2.0數(shù)據(jù)教程之六:編程設(shè)置ObjectDataSource的參數(shù)值

ASP.NET 2.0數(shù)據(jù)教程之七:使用DropDownList過濾的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之八:使用兩個(gè)DropDownList過濾的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之九:跨頁面的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之十:使用 GridView 和DetailView實(shí)現(xiàn)的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之十一:基于數(shù)據(jù)的自定義格式化

ASP.NET 2.0數(shù)據(jù)教程之十二:在GridView控件中使用TemplateField


Step 8:RowDataBound事件處理中編碼確定數(shù)據(jù)對(duì)應(yīng)的值

當(dāng)ProductsDataTable綁定到GridView,GridView將會(huì)產(chǎn)生若干個(gè)ProductsRow。GridViewRowDataItem屬性將會(huì)生成一個(gè)實(shí)際的ProductRow。在GridView RowDataBound事件發(fā)生之后,為了確定UnitsInStock的值,我們需要?jiǎng)?chuàng)建RowDataBound的事件處理,在其中我們可以確定UnitsInStock的值并做相應(yīng)的格式化

EventHandler的創(chuàng)建過程和前面兩個(gè)一樣


10: 創(chuàng)建GridViewRowDataBound事件的事件處理

在后臺(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 < 10m)

        {

            // TODO: Highlight the row yellow...

       }

    }

當(dāng)運(yùn)用RowDataBound事件處理時(shí),GridView由各種類型不同的行組成,而事件發(fā)生針對(duì)所有的行類型, GridViewRow的類型可以由RowType屬性決定,可以是以下類型中的一種

·         DataRow GridViewDataSource中的一條記錄

·         EmptyDataRowGridViewDataSource顯示出來的某一行為空

·         Footer 底部行; 顯示由GridViewShowFooter屬性決定

·         Header 頭部行; 顯示由GridViewShowHeader屬性決定

·         Pager GridView的分頁,這一行顯示分頁的標(biāo)記

·         Separator 對(duì)于GridView不可用,但是對(duì)于DataListReapterRowType屬性卻很有用,我們將在將來的文章中討論他們

當(dāng)上面四種(DataRow, Pager Rows Footer, Header)都不合適對(duì)應(yīng)值時(shí),將返回一個(gè)空的數(shù)據(jù)項(xiàng), 所以我們需要在代碼中檢查GridViewRowRowType屬性來確定:

 

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 < 10m)

            {

                // TODO: Highlight row yellow...

            }

        }

}

 

系列文章導(dǎo)航:

ASP.NET 2.0數(shù)據(jù)教程之一:創(chuàng)建一個(gè)數(shù)據(jù)訪問層

ASP.NET 2.0數(shù)據(jù)教程之二:創(chuàng)建一個(gè)業(yè)務(wù)邏輯層

ASP.NET 2.0數(shù)據(jù)教程之三:母板頁和站點(diǎn)導(dǎo)航

ASP.NET 2.0數(shù)據(jù)教程之四:使用ObjectDataSource展現(xiàn)數(shù)據(jù)

ASP.NET 2.0數(shù)據(jù)教程之五:聲明參數(shù)

ASP.NET 2.0數(shù)據(jù)教程之六:編程設(shè)置ObjectDataSource的參數(shù)值

ASP.NET 2.0數(shù)據(jù)教程之七:使用DropDownList過濾的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之八:使用兩個(gè)DropDownList過濾的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之九:跨頁面的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之十:使用 GridView 和DetailView實(shí)現(xiàn)的主/從報(bào)表

ASP.NET 2.0數(shù)據(jù)教程之十一:基于數(shù)據(jù)的自定義格式化

ASP.NET 2.0數(shù)據(jù)教程之十二:在GridView控件中使用TemplateField


Step 9:用黃色高亮那些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 < 10m)

            {

                e.Row.CssClass = "AffordablePriceEmphasis";

            }

        }

    }


圖 11: 所需要的行用高亮黃色顯示

總結(jié)

在本篇中我們演示了基于數(shù)據(jù)綁定來自定義格式化GridView, DetailsView, FormView的方法。為了完成這些,我們創(chuàng)建DataBound或者RowDataBound事件,為了訪問DetailsViewFormView的數(shù)據(jù)綁定,我們可以通過DataItem屬性。對(duì)于GridView,每個(gè)GridViewRow實(shí)例的DataItem屬性包含了綁定的數(shù)據(jù)(RowDataBound事件處理中可用)

為了調(diào)整格式,我們可能需要訪問某一特定的行,在GridViewDetailsView中我們可以通過索引訪問,而在FormView中我們則需要用FindControl("controlID"),同時(shí)FindControl("controlID")通常都可以訪問Web控件Tempelete中的某個(gè)控件

在下一篇中我們將討論如何在GridViewDetailsView使用Tempeletes, 還將討論另外一些自定義格式化的方法

祝編程快樂!

作者簡(jiǎn)介

Scott Mitchell,著有六本ASP/ASP.NET方面的書,是4GuysFromRolla.com的創(chuàng)始人,自1998年以來一直應(yīng)用 微軟Web技術(shù)。Scott是個(gè)獨(dú)立的技術(shù)咨詢顧問,培訓(xùn)師,作家,最近完成了將由Sams出版社出版的新作,24小時(shí)內(nèi)精通ASP.NET 2.0。他的聯(lián)系電郵為mitchell@4guysfromrolla.com,也可以通過他的博客http://ScottOnWriting.NET與他聯(lián)系。

 

NET技術(shù)ASP.NET 2.0數(shù)據(jù)教程之十一:基于數(shù)據(jù)的自定義格式化,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 昌都县| 永泰县| 会理县| 罗定市| 西充县| 柳林县| 城口县| 石河子市| 四平市| 衡南县| 额尔古纳市| 易门县| 库伦旗| 闻喜县| 徐水县| 三门县| 巴林左旗| 密山市| 东阳市| 彭阳县| 河西区| 佛山市| 诸暨市| 塔城市| 南召县| 济阳县| 原平市| 钟山县| 固安县| 宁国市| 南华县| 平阴县| 遵义市| 华池县| 兰考县| 潮安县| 贵定县| 桃园市| 鄯善县| 平利县| 合江县|