DisplayFormatAttribute 類別

定義

指定 ASP.NET Dynamic Data 顯示和格式化資料欄位的方式。

public ref class DisplayFormatAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false)]
public class DisplayFormatAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false)>]
type DisplayFormatAttribute = class
    inherit Attribute
Public Class DisplayFormatAttribute
Inherits Attribute
繼承
DisplayFormatAttribute
屬性

範例

請參閱此功能的線上範例: 執行

下列範例示範如何使用 DisplayFormatAttribute 來自訂資料欄位的格式。 此範例會執行下列步驟:

  • 實作中繼資料部分類別和相關聯的中繼資料類別。

  • 在相關聯的中繼資料類別中,它會套用 DisplayFormatAttribute 屬性來指定下列結果:

    • 當資料欄位是空的時,顯示文字 「[Null]」。

    • 以地區設定特定的貨幣格式顯示貨幣資料。

    • 以簡短格式顯示日期資訊, (mm/dd/yy) 。 此格式也適用于編輯模式。

using System;
using System.Web.DynamicData;
using System.ComponentModel.DataAnnotations;

[MetadataType(typeof(ProductMetaData))]
public partial class Product
{
}

public class ProductMetaData
{
    
    // Applying DisplayFormatAttribute
    // Display the text [Null] when the data field is empty.
    // Also, convert empty string to null for storing.
    [DisplayFormat(ConvertEmptyStringToNull = true, NullDisplayText = "[Null]")]
    public object Size;

    // Display currency data field in the format $1,345.50.
    [DisplayFormat(DataFormatString="{0:C}")]
    public object StandardCost;

    // Display date data field in the short format 11/12/08.
    // Also, apply format in edit mode.
    [DisplayFormat(ApplyFormatInEditMode=true, DataFormatString = "{0:d}")]
    public object SellStartDate;
}
Imports System.Web.DynamicData
Imports System.ComponentModel.DataAnnotations


<MetadataType(GetType(ProductMetaData))> _
Partial Public Class Product

End Class

Public Class ProductMetaData
   
    ' Applying DisplayFormatAttribute

    ' Display the text [Null] when the data field is empty.
    ' Also, convert empty string to null for storing.
    <DisplayFormat(ConvertEmptyStringToNull:=True, NullDisplayText:="[Null]")> _
    Public Size As Object

    ' Display currency data field in the format such as $1,345.50.
    <DisplayFormat(DataFormatString:="{0:C}")> _
    Public StandardCost As Object

    ' Display date data field in the short format such as 11/12/08.
    ' Also, apply format in edit mode.
    <DisplayFormat(ApplyFormatInEditMode:=True, DataFormatString:="{0:d}")> _
    Public SellStartDate As Object

End Class

若要編譯範例,您需要下列專案:

  • 任何版本的 Visual Studio 2010 或更新版本。

  • AdventureWorksLT 範例資料庫。 如需如何下載及安裝SQL Server範例資料庫的資訊,請參閱Microsoft SQL Server 產品範例:GitHub 上的資料庫。 請確定您為所執行SQL Server版本安裝正確的範例資料庫版本。

  • 資料驅動網站。 這可讓您建立資料庫的資料內容,以及包含要自訂之資料欄位的 類別。 如需詳細資訊,請參閱Walkthrough: Creating a New Dynamic Data Web Site using Scaffolding

備註

當您將此屬性套用至資料欄位時,您必須遵循使用屬性的指導方針。 如需詳細資訊,請參閱 ASP.NET 動態資料指導方針

建構函式

DisplayFormatAttribute()

初始化 DisplayFormatAttribute 類別的新執行個體。

屬性

ApplyFormatInEditMode

取得或設定值,這個值指出當資料欄位處於編輯模式時,DataFormatString 屬性指定的格式化字串是否套用至欄位值。

ConvertEmptyStringToNull

取得或設定值,這個值指出在資料來源中更新資料欄位時,是否將空字串值 ("") 自動轉換為 null

DataFormatString

取得或設定欄位值的顯示格式。

HtmlEncode

取得或設定值,這個值指出欄位是否應經過 HTML 編碼。

NullDisplayText

取得或設定欄位值為 null 時為欄位顯示的文字。

NullDisplayTextResourceType

取得或設定 Type,其中包含 NullDisplayText 的資源。

搭配 NullDisplayText 使用 NullDisplayTextResourceType,可允許 GetNullDisplayText() 方法傳回當地語系化的值。

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetNullDisplayText()

傳回 NullDisplayText 的 UI 顯示字串。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱