通过


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示例数据库的信息,请参阅 GitHub 上的 Microsoft SQL Server Product Samples: Database。 请确保为正在运行的 SQL Server 版本安装示例数据库的正确版本。
  • 数据驱动的网站。 这样,便可以为数据库和包含要自定义的数据字段的类创建数据上下文。 有关详细信息,请参阅 Walkthrough: Creating a New Dynamic Data Web Site using Scaffolding

注解

将此属性应用于数据字段时,必须遵循属性的使用准则。

构造函数

名称 说明
DisplayFormatAttribute()

初始化 DisplayFormatAttribute 类的新实例。

属性

名称 说明
ApplyFormatInEditMode

获取或设置一个值,该值指示当数据字段处于编辑模式时,属性指定的 DataFormatString 格式字符串是否应用于字段值。

ConvertEmptyStringToNull

获取或设置一个值,该值指示在数据源中更新数据字段时是否自动转换为 null 空字符串值(“”)。

DataFormatString

获取或设置字段值的显示格式。

HtmlEncode

获取或设置一个值,该值指示字段是否应进行 HTML 编码。

NullDisplayText

获取或设置字段值 null时为字段显示的文本。

NullDisplayTextResourceType

获取或设置 Type 包含其资源的资源 NullDisplayText

通过 NullDisplayTextResourceType 结合使用 NullDisplayText,该方法可以 GetNullDisplayText() 返回本地化值。

TypeId

在派生类中实现时,获取此 Attribute的唯一标识符。

(继承自 Attribute)

方法

名称 说明
Equals(Object)

返回一个值,该值指示此实例是否等于指定对象。

(继承自 Attribute)
GetHashCode()

返回此实例的哈希代码。

(继承自 Attribute)
GetNullDisplayText()

返回用于 . 的 NullDisplayTextUI 显示字符串。

GetType()

获取当前实例的 Type

(继承自 Object)
IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否为派生类的默认值。

(继承自 Attribute)
Match(Object)

在派生类中重写时,返回一个值,该值指示此实例是否等于指定对象。

(继承自 Attribute)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
ToString()

返回一个表示当前对象的字符串。

(继承自 Object)

显式接口实现

名称 说明
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。

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

检索对象的类型信息,该信息可用于获取接口的类型信息。

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

检索对象提供的类型信息接口的数量(0 或 1)。

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

提供对对象公开的属性和方法的访问。

(继承自 Attribute)

适用于