溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

ASP.NET 2.0中怎么格式化DataList和Repeater數據

發布時間:2021-07-15 16:45:39 來源:億速云 閱讀:267 作者:Leah 欄目:開發技術

這篇文章將為大家詳細講解有關ASP.NET 2.0中怎么格式化DataList和Repeater數據,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

第一步: 在DataList顯示Product  信息

  在學習格式化之前,我們首先創建一個使用DataList顯示product信息的頁面.在前面一章里,我們創建了一個ItemTemplate顯示product 的name,category, supplier, quantity和price的DataList.我們在本章來重復做一次.你可以重新創建DataList和它的ObjectDataSource ,或者直接把前面一章里的Basics.aspx里的控件復制到本章的頁面(Formatting.aspx)里.當你完成了Formatting.aspx后,將DataList的ID從DataList1改為ItemDataBoundFormattingExample.下面,在瀏覽器里看看DataList.如圖1所示,唯一的格式在于每個product的交替的背景色.

ASP.NET 2.0中怎么格式化DataList和Repeater數據

圖 1: 在DataList 里列出product信息

在本章教程里,我們來將價格小于 $20.00 的product的名字和單價用黃色 高亮來顯示.

第二步: 在 ItemDataBound Event Handler里編程判斷數據的值

  由于只有價格低于$20.00 的product會被格式化,因此我們首先要判斷每個product的價格.在綁定數據到DataList時,DataList 為每條數據源的記錄創建一個DataListItem實例,并綁定數據.當記錄綁定到DataListItem對象后,ItemDataBound事件被激發.我們可以為這個事件創建一個event handler來判斷當前DataListItem的值,再根據這個值來格式化數據.
添加以下代碼為DataList創建ItemDataBound事件

protected void ItemDataBoundFormattingExample_ItemDataBound
 (object sender, DataListItemEventArgs e)
{
 if (e.Item.ItemType == ListItemType.Item ||
 e.Item.ItemType == ListItemType.AlternatingItem)
 {
 // Programmatically reference the ProductsRow instance bound
 // to this DataListItem
 Northwind.ProductsRow product =
  (Northwind.ProductsRow)((System.Data.DataRowView)e.Item.DataItem).Row;
 // See if the UnitPrice is not NULL and less than $20.00
 if (!product.IsUnitPriceNull() && product.UnitPrice < 20)
 {
  // TODO: Highlight the product's name and price
 }
 }
}

  DataList的ItemDataBound event handler在概念和語義上來說,和GridView的RowDataBound event handler一樣(見基于數據的自定義格式化),語法上有一點差別.當ItemDataBound事件激發時,剛剛綁定數據的DataListItem通過e.Item(在GridView里是e.Row和RowDataBound)傳遞給相關的event handler.DataList的ItemDataBound event handler影響到每一行,包括 header , footer 和separator.但是product信息只綁定到data行.因此,在處理ItemDataBound事件前,我們首先要判斷處理的是否是data行.這個可以通過檢查DataListItem的ItemType 屬性來完成,它可以有以下八個值:

AlternatingItem
EditItem
Footer
Header
Item
Pager
SelectedItem
Separator

  Item和AlternatingItem都表示DataList的data item.假設我們在處理Item或AlternatingItem,我們可以獲取綁定到當前DataListItem的ProductsRow的實例.DataListItem的DataItem屬性包含了DataRowView對象的引用,通過它的Row屬性可以獲取ProductsRow對象.

  下面我們來檢查ProductsRow實例的單價屬性.由于Product表的UnitPrice字段允許空值,所以在獲取UnitPrice屬性前我們應該先用IsUnitPriceNull()方法檢查這個值是否為空.如果不是,我們再檢查看它是否低于$20.00.如果是,我們就進行格式化處理.

第三步: 是Product的 Name 和Price高亮顯示

  一旦我們發現Product的price低于$20.00,我們將使它的name和price顯示高亮.首先我們要編程獲得ItemTemplate里顯示Product的name和price的Label控件.然后我們將它的背景色顯示為黃色.這個可以直接通過修改Label空間的BackColor屬性(LabelID.BackColor = Color.Yellow).當然最理想的做法是所有的顯示相關的行為都通過CSS來實現.實際上我們在基于數據的自定義格式化一章里創建的Styles.css - AffordablePriceEmphasis已經提供了這個功能.

使用以下代碼設置兩個Label控件的CssClass 屬性為AffordablePriceEmphasis來完成格式化:

// Highlight the product name and unit price Labels
// First, get a reference to the two Label Web controls
Label ProductNameLabel = (Label)e.Item.FindControl("ProductNameLabel");
Label UnitPriceLabel = (Label)e.Item.FindControl("UnitPriceLabel");
// Next, set their CssClass properties
if (ProductNameLabel != null)
 ProductNameLabel.CssClass = "AffordablePriceEmphasis";
if (UnitPriceLabel != null)
 UnitPriceLabel.CssClass = "AffordablePriceEmphasis";

ItemDataBound 事件完成后,在瀏覽器里瀏覽Formatting.aspx頁.如圖2所示,價格低于 $20.00 的product的name和prict都高亮顯示了.

ASP.NET 2.0中怎么格式化DataList和Repeater數據

圖2: 價格低于$20.00 的product都被高亮顯示

  注意:由于DataList使用 HTML <table>, DataListItem實例有可以設置整個item風格的屬性.比如,如果我們想在price低于$20.00時將所有的item都用黃色來高亮顯示,我們可以用e.Item.CssClass = "AffordablePriceEmphasis"來代替上面的代碼(見圖3).

  而組成Repeater的RepeaterItem并沒有提供這樣的屬性.因此,在Repeater里自定義格式需要設置templates里的控件的格式,象在圖2里所做的那樣.

ASP.NET 2.0中怎么格式化DataList和Repeater數據

圖 3: The Entire Product Item is Highlighted for Products Under $20.00

使用 Template的格式化功能

  在在GridView控件中使用TemplateField 一章里,我們學習了如何使用GridView TemplateField的格式化功能來格式化GridView的數據.格式化功能是一種可以從template里調用并返回HTML顯示的方法.格式化功能可以寫在ASP.NET page的 code-behind class 或App_Code 文件夾里的類文件里或單獨的類庫項目里.如果你想在其它ASP.NET web程序或多個ASP.NET 頁用到同樣的功能,那么不要把它下在ASP.NET page的 code-behind class 里.

  為了演示這個功能,我們將修改product信息.如果product被停用,我們在product的name后面增加一個“[DISCONTINUED]”的text.同樣的,如果price低于 $20.00 我們將會用黃色來高亮顯示(如我們在ItemDataBound event handler例子里做的那樣).如果price等于或高于 $20.00,我們將不顯示實際的價格,而是在text里顯示“Please call for a price quote”. 圖4是完成以上功能的頁面截圖.

ASP.NET 2.0中怎么格式化DataList和Repeater數據

圖 4: 將比較貴的Products 的價格用文本“Please call for a price quote”來代替.

第一步: 創建格式化功能

  這個例子里我們需要兩個格式化功能,其一是在被停用的product name后面加上“[DISCONTINUED]”, 其二是對價格低于$20.00的product高亮顯示,其它則顯示“Please call for a price quote”.我們將在ASP.NET page的code-behind class 里創建這些功能,并給它們取名為DisplayProductNameAndDiscontinuedStatus 和DisplayPrice.這兩個方法都需要返回HTML,而且為了在ASP.NET page的聲明語法里調用,都需要標記為Protected (or Public).下面是這兩個方法的代碼:

protected string DisplayProductNameAndDiscontinuedStatus
 (string productName, bool discontinued)
{
 // Return just the productName if discontinued is false
 if (!discontinued)
  return productName;
 else
  // otherwise, return the productName appended with the text "[DISCONTINUED]"
  return string.Concat(productName, " [DISCONTINUED]");
}
protected string DisplayPrice(Northwind.ProductsRow product)
{
 // If price is less than $20.00, return the price, highlighted
 if (!product.IsUnitPriceNull() && product.UnitPrice < 20)
  return string.Concat("<span class=\"AffordablePriceEmphasis\">",
        product.UnitPrice.ToString("C"), "</span>");
 else
  // Otherwise return the text, "Please call for a price quote"
  return "<span>Please call for a price quote</span>";
}

  注意到DisplayProductNameAndDiscontinuedStatus 方法接收productName 和discontinued 的值.而DisplayPrice 方法接收ProductsRow (而不是UnitPrice).如果格式化功能處理可能包含數據庫空值(比如UnitPrice,而ProductName和Discontinued都不允許空)的量值,要特別小心處理.

  輸入的值可能是一個DBNull而不是你期望的數據類型,因此輸入參數的類型必須為Object.而且比如檢查傳進來的值是否為database NULL.也就是說,如果我們想讓DisplayPrice 方法以價格為參數,我們需要以下代碼:

protected string DisplayPrice(object unitPrice)
{
 // If price is less than $20.00, return the price, highlighted
 if (!Convert.IsDBNull(unitPrice) && ((decimal) unitPrice) < 20)
  return string.Concat("<span class=\"AffordablePriceEmphasis\">",
        ((decimal) unitPrice).ToString("C"), "</span>");
 else
  // Otherwise return the text, "Please call for a price quote"
  return "<span>Please call for a price quote</span>";
}

注意輸入參數UnitPrice的類型為Object,條件判斷語句被修改為判斷unitPrice 是否為DBNull.而且,由于UnitPrice是作為Object傳進來的,所以必須要類型轉換為decimal.

第二步: 在DataList 的ItemTemplate調用格式化方法

在完成以上代碼后,剩下的工作就是在DataList的ItemTemplate里調用這些格式化功能.我們需要使用以下代碼:

<%# MethodName(inputParameter1, inputParameter2, ...) %>

  在DataList的ItemTemplate里,ProductNameLabel Label通過指定text屬性為<%# Eval("ProductName") %>顯示的product的name.為了在需要的情況下加上“[DISCONTINUED]” ,修改代碼,使用DisplayProductNameAndDiscontinuedStatus 方法來指定text屬性.我們需要使用Eval("columnName") 語法來將product的name和discontinued的值傳進去.Eval 返回的值為Object類型,而DisplayProductNameAndDiscontinuedStatus 的參數為String 和Boolean.因此,我們需要將Eval 方法返回的值轉換為需要的參數類型,代碼如下:

<h5>
 <asp:Label ID="ProductNameLabel" runat="server"
  Text='<%# DisplayProductNameAndDiscontinuedStatus((string) Eval("ProductName"),
    (bool) Eval("Discontinued")) %>'>
 </asp:Label>
</h5>

和顯示product的name和“[DISCONTINUED]” 文本一樣,我們設置UnitPriceLabel label的屬性為DisplayPrice 的返回值來顯示價格.我們將ProductsRow作為參數,而不是UnitPrice:

<asp:Label ID="UnitPriceLabel" runat="server"
 Text='<%# DisplayPrice((Northwind.ProductsRow)
   ((System.Data.DataRowView) Container.DataItem).Row) %>'>
</asp:Label>

完成以上代碼后,在瀏覽器里看一下頁面.你的頁面應該和圖5看起來差不多.

ASP.NET 2.0中怎么格式化DataList和Repeater數據

關于ASP.NET 2.0中怎么格式化DataList和Repeater數據就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女