更新:2007 年 11 月
本主題說明如何建立自己的欄位範本,從而在 ASP.NET Dynamic Data 中自訂資料欄位 (資料表資料行) 的顯示。本主題會在接下來的小節中詳細描述這些工作:
建立自訂欄位範本以自訂資料欄位的顯示。
將自訂欄位範本與資料欄位建立關聯。如此可以在處理顯示的資料欄位與自訂欄位範本之間,建立資料模型關聯。
注意事項:當您使用資料模型自訂資料欄位時,自訂內容會套用到整個網站中。這表示動態資料會使用自訂資料欄位來顯示資料欄位,而不會使用根據資料欄位型別選取的預設範本。
若要建立自訂欄位範本
在 [方案總管] 中,以滑鼠右鍵按一下 [DynamicData/FieldTemplates] 資料夾,然後按一下 [加入新項目]。
在 [已安裝的範本],按一下 [欄位範本]。
在 [名稱] 方塊中,輸入任何您想要使用的控制項名稱。同時,請務必選取 [將程式碼置於個別檔案中] 方塊。
切換到或開啟剛剛建立的使用者控制項的檔案。
在 @ Control 指示詞中,加入參考程式碼後置的檔案的 CodeFile 屬性以及參考控制項類別的 Inherits 屬性。
<%@ Control Language="C#" CodeFile=MyCustomControl.ascx.cs" Inherits="MyCustomControl" %><%@ Control Language="VB" CodeFile=MyCustomControl.ascx.cs" Inherits="MyCustomControl" %>建立會被呈現的標記以顯示資料。
下列範例中的 Label 控制項,其 Text 屬性會設為目前欄位值字串,且其 OnDataBinding 屬性會設為自訂事件處理常式。
<asp:Label id="TextLabel1" OnDataBinding="DataBindingHandler" Text='<%# FieldValueString %>'> </asp:Label>儲存並關閉使用者控制項檔案。
開啟使用者控制項的程式碼後置檔案。
使用 Visual Basic 中的 Imports 關鍵字或 Visual C# 中的 using 關鍵字,加入參考 System.Web.DynamicData 的命名空間指示詞。
using System.Web.DynamicData;Imports System.Web.DynamicData自 FieldTemplateUserControl 類別衍生使用者控制項部分類別,如下程式碼文字。
partial class MyCustomControl: FieldTemplateUserControl{ }
Public Partial Class MyCustomControl Inherits _ FieldTemplateUserControl End Class若要自訂控制項顯示資料欄位的方法,您需要處理使用者控制項的 OnDataBinding 事件。在處理常式中,您可以從控制項的 FieldValue 屬性取得目前資料欄位的值,然後根據內容自訂顯示。
下列範例將示範如何處理 OnDataBinding 事件。
public void DataBindingHandler(object sender, EventArgs e) { // Define the understocked threshold. short underStockedThreshold = 150; // Get the current number of items in stock. short currentValue = (short)FieldValue; // Check product stock. if (currentValue < underStockedThreshold) { // Customize display here. For example you show the data with //a red background. } }Public Sub DataBindingHandler(ByVal sender As Object, _ ByVal e As EventArgs) ' Define the understocked threshold. Dim underStockedThreshold As Short = 150 ' Get the current number of items in stock. Dim currentValue As Short = DirectCast(FieldValue, Short) ' Check product stock. If currentValue < underStockedThreshold Then 'Customize display here. For example you show the data with 'a red background. End If End Sub關閉使用者控制項的程式碼後置檔案。現在,您已完成自訂欄位範本的建立。
若要將自訂欄位範本與資料欄位建立關聯
在 [方案總管] 中,以滑鼠右鍵按一下 [App_Code] 資料夾,然後按一下 [加入新項目]。
在 [已安裝的範本],按一下 [類別]。
在 [名稱] 方塊中,輸入資料庫資料表 (包含要顯示之自訂欄位範本的資料) 的名稱。
例如,如果自訂控制項會顯示 Products 資料表中的資料,檔案名稱即為 Products.cs 或 Product.vb,且類別名稱為 Product。這個檔案也會包含可以讓您自訂資料欄位顯示的輔助欄位。
切換到或開啟剛剛建立的類別檔案。
在 Visual Basic 中將 Partial 關鍵字加入類別定義,或在 Visual C# 中將 partial 關鍵字加入類別定義,使其成為部分類別。
使用 Visual Basic 中的 Imports 關鍵字或 Visual C# 中的 using關鍵字,加入參考 System.Web.DynamicData 與 System.ComponentModel.DataAnnotations 的命名空間指示詞。
using System.Web.DynamicData; using System.ComponentModel.DataAnnotations;Imports System.Web.DynamicData Imports System.ComponentModel.DataAnnotations將 MetadataTypeAttribute 屬性加入至部分類別定義。屬性的參數為輔助中繼資料類別的名稱,您會在稍後建立這個輔助中繼資料類別以處理資料欄位顯示的自訂。
[MetadataType(typeof(ProductMetadata))] public partial class Product { }<MetadataType(GetType(ProductMetadata))> _ Public Partial Class Product End Class建立要當做輔助中繼資料類別的類別。該類別可以使用任一名稱,但是類別名稱必須符合前一個步驟中所參考之 MetadataTypeAttribute 屬性的名稱。
在中繼資料類別中,建立名稱對應於要顯示之資料欄位的名稱。使用 UIHintAttribute 屬性標記該欄位,指定要用於顯示的自訂欄位範本名稱。
輔助類別的唯一用意在於提供可以定義 UIHintAttribute 屬性的空間,如此,您便不需要在類別中加入任何程式碼內容。
下列範例為完整的中繼資料定義,其中包含具有 UIHintAttribute 屬性的單一欄位,而這個欄位定義 UnitsInStock 欄位的自訂顯示。
public class ProductMetadata [UIHint("UnitsInStock")] public object UnitsInStock; }Public Class ProductMetadata <UIHint("UnitsInStock")> _ Public UnitsInStock As Object End Class
範例
下列範例顯示在檢查產品庫存量的自訂欄位範本。如果產品的庫存過低,欄位範本會以紅色前景顯示值。
<%@ Control Language="VB" CodeFile="UnitsInStock.ascx.vb"
Inherits="DynamicData_FieldTemplates_UnitsInStock" %>
<asp:Label id="TextLabel1" OnDataBinding="DataBindingHandler"
Text='<%# FieldValueString %>' ></asp:Label>
<%@ Control Language="C#" CodeFile="UnitsInStock.ascx.cs"
Inherits="DynamicData_FieldTemplates_UnitsInStock" %>
<asp:Label id="TextLabel1" OnDataBinding="DataBindingHandler"
Text='<%# FieldValueString %>' ></asp:Label>
Imports System.Web.DynamicData
Partial Public Class DynamicData_FieldTemplates_UnitsInStock
Inherits FieldTemplateUserControl
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
' DataBinding event handler.
Public Sub DataBindingHandler(ByVal sender As Object, _
ByVal e As EventArgs)
' Define product understocked threshold.
Dim underStockedThreshold As Short = 150
' Get the current number of items in stock.
Dim currentValue As Short = DirectCast(FieldValue, Short)
' Check product stock.
If currentValue < underStockedThreshold Then
' The product is understocked, set its
' foreground color to red.
TextLabel1.ForeColor = System.Drawing.Color.Red
End If
End Sub
End Class
using System.Web.DynamicData;
public partial class DynamicData_FieldTemplates_UnitsInStock : FieldTemplateUserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
// DataBinding event handler.
public void DataBindingHandler(object sender, EventArgs e)
{
// Define product understocked threshold.
short underStockedThreshold = 150;
// Get the current number of items in stock.
short currentValue = (short)FieldValue;
// Check product stock.
if (currentValue < underStockedThreshold)
{
// The product is understocked, set its
// foreground color to red.
TextLabel1.ForeColor = System.Drawing.Color.Red;
}
}
}
編譯程式碼
Microsoft Visual Studio 2008 Service Pack 1 或 Visual Web Developer 2008 Express 版 Service Pack 1。
AdventureWorksLT 範例資料庫。如需下載並安裝 SQL Server 範例資料庫,請參閱 CodePlex 網站上的 Microsoft SQL Server 產品範例:資料庫 (英文)。請確認已配合您所執行的 SQL Server 版本 (Microsoft SQL Server 2005 或 Microsoft SQL Server 2008) 安裝版本正確的範例資料庫。
Dynamic Data 網站。這可以讓您建立資料庫的資料內容,以及包含可以自訂之資料欄位的類別與要覆寫的方法。此外,也會建立要使用先前說明之頁面的環境。如需詳細資訊,請參閱逐步解說:建立使用 Scaffolding 的新動態資料網站。