語言

DisplayFormatAttribute 類別

定義

規範 ASP.NET 動態資料如何顯示與格式化資料欄位。

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樣本資料庫,請參閱GitHubMicrosoft SQL Server產品樣本:資料庫。 確保你安裝的是與你所使用的 SQL Server 版本相符的範例資料庫版本。
  • 一個以數據為基礎的網站。 這讓你能為資料庫建立一個資料上下文,以及包含資料欄位的類別,以便自訂。 如需詳細資訊,請參閱Walkthrough: Creating a New Dynamic Data Web Site using Scaffolding。

備註

當你將此屬性套用到資料欄位時,必須遵循屬性使用的指引。 欲了解更多資訊,請參見 ASP.NET 動態資料指引。

建構函式

名稱 Description
DisplayFormatAttribute()

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

屬性

名稱 Description
ApplyFormatInEditMode

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

ConvertEmptyStringToNull

取得或設定一個值,指示資料欄位更新時,空字串值(「」)是否會自動轉換成 null 。

DataFormatString

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

HtmlEncode

取得或設定一個值,指示欄位是否應該以 HTML 編碼。

NullDisplayText

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

NullDisplayTextResourceType

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

使用 NullDisplayTextResourceType 與 NullDisplayText,可使 GetNullDisplayText() 方法返回局部化值。

TypeId

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

(繼承來源 Attribute)

方法

名稱 Description
Equals(Object)

傳回值,這個值表示這個實例是否等於指定的物件。

(繼承來源 Attribute)
GetHashCode()

傳回這個實例的哈希碼。

(繼承來源 Attribute)
GetNullDisplayText()

回傳 的 UI 顯示字串。NullDisplayText

GetType()

取得目前實例的 Type。

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,指出這個實例的值是否為衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,傳回值,指出這個實例是否等於指定的物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

明確介面實作

名稱 Description
_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)

適用於

另請參閱