DisplayFormatAttribute 类  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定 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 AttributePublic Class DisplayFormatAttribute
Inherits Attribute- 继承
- 属性
示例
请参阅此功能的联机示例: 运行。
以下示例演示如何使用 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 | 获取或设置一个值,该值指示在数据源中更新数据字段时是否将空字符串值 ("") 自动转换为  | 
| DataFormatString | 获取或设置字段值的显示格式。 | 
| HtmlEncode | 获取或设置一个值,该值指示字段是否应经过 HTML 编码。 | 
| NullDisplayText | 获取或设置字段值为  | 
| NullDisplayTextResourceType | 获取或设置包含 NullDisplayText 的资源的 Type。 配合使用 NullDisplayTextResourceType 与 NullDisplayText,允许 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) | 将一组名称映射为对应的一组调度标识符。(继承自 Attribute) | 
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) | 检索对象的类型信息,然后可以使用该信息获取接口的类型信息。(继承自 Attribute) | 
| _Attribute.GetTypeInfoCount(UInt32) | 检索对象提供的类型信息接口的数量(0 或 1)。(继承自 Attribute) | 
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) | 提供对某一对象公开的属性和方法的访问。(继承自 Attribute) |