DisplayFormatAttribute 类

定义

指定 ASP.NET 动态数据如何显示数据字段以及如何设置数据字段的格式。Specifies how data fields are displayed and formatted by 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
属性

示例

请参阅此功能的联机示例: 运行See an online example of this feature: Run.

下面的示例演示如何使用 DisplayFormatAttribute 自定义数据字段的格式。The following example shows how to use the DisplayFormatAttribute to customize formatting for a data field. 该示例执行以下步骤:The example performs the following steps:

  • 实现元数据分部类和关联的元数据类。Implements a metadata partial class and an associated metadata class.

  • 在关联的元数据类中,它应用 DisplayFormatAttribute 特性来指定以下结果:In the associated metadata class, it applies the DisplayFormatAttribute attribute to specify the following results:

    • 如果数据字段为空,则显示文本 "[Null]"。Display the text "[Null]" when a data field is empty.

    • 以特定于区域设置的货币格式显示货币数据。Display currency data in locale specific currency format.

    • 以短格式显示日期信息 (mm/dd/yy) 。Display date information in short format (mm/dd/yy). 此格式在编辑模式下也适用。This format also applies in edit mode.

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

若要编译该示例,需要以下各项:To compile the example, you need the following:

  • 任何版本的 Visual Studio 2010 或更高版本。Any edition of Visual Studio 2010 or later.

  • AdventureWorksLT 示例数据库。The AdventureWorksLT sample database. 有关如何下载和安装 SQL Server 示例数据库的信息,请参阅 GitHub 上的 Microsoft SQL Server 产品示例:数据库For information about how to download and install the SQL Server sample database, see Microsoft SQL Server Product Samples: Database on GitHub. 请确保为运行的 SQL Server 版本安装了正确版本的示例数据库。Make sure that you install the correct version of the sample database for the version of SQL Server that you are running.

  • 数据驱动的网站。A data-driven Web site. 这使您可以为数据库创建数据上下文,并创建包含要自定义的数据字段的类。This enables you to create a data context for the database and the class that contains the data field to customize. 有关详细信息,请参阅 Walkthrough: Creating a New Dynamic Data Web Site using ScaffoldingFor more information, see Walkthrough: Creating a New Dynamic Data Web Site using Scaffolding.

注解

将此特性应用于数据字段时,必须遵循这些特性的使用准则。When you apply this attribute to a data field, you must follow the guidelines for the use of the attributes. 有关详细信息,请参阅 ASP.NET 动态数据准则For more information, see ASP.NET Dynamic Data Guidelines.

构造函数

DisplayFormatAttribute()

初始化 DisplayFormatAttribute 类的新实例。Initializes a new instance of the DisplayFormatAttribute class.

属性

ApplyFormatInEditMode

获取或设置一个值,该值指示数据字段处于编辑模式时,是否将 DataFormatString 属性指定的格式设置字符串应用于字段值。Gets or sets a value that indicates whether the formatting string that is specified by the DataFormatString property is applied to the field value when the data field is in edit mode.

ConvertEmptyStringToNull

获取或设置一个值,该值指示在数据源中更新数据字段时是否将空字符串值 ("") 自动转换为 nullGets or sets a value that indicates whether empty string values ("") are automatically converted to null when the data field is updated in the data source.

DataFormatString

获取或设置字段值的显示格式。Gets or sets the display format for the field value.

HtmlEncode

获取或设置一个值,该值指示字段是否应经过 HTML 编码。Gets or sets a value that indicates whether the field should be HTML-encoded.

NullDisplayText

获取或设置字段值为 null 时为字段显示的文本。Gets or sets the text that is displayed for a field when the field's value is null.

NullDisplayTextResourceType

获取或设置包含 NullDisplayText 的资源的 TypeGets or sets the Type that contains the resources for NullDisplayText.

配合使用 NullDisplayTextResourceTypeNullDisplayText,允许 GetNullDisplayText() 方法返回已本地化的值。Using NullDisplayTextResourceType along with NullDisplayText, allows the GetNullDisplayText() method to return localized values.

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(继承自 Attribute)

方法

Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object.

(继承自 Attribute)
GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

(继承自 Attribute)
GetNullDisplayText()

返回 NullDisplayText 的 UI 显示字符串。Returns the UI display string for NullDisplayText.

GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(继承自 Attribute)
Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(继承自 Attribute)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

显式接口实现

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

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(继承自 Attribute)

适用于