通过


DataTypeAttribute 类

定义

指定要与数据字段关联的其他类型的名称。

public ref class DataTypeAttribute : System::ComponentModel::DataAnnotations::ValidationAttribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Method | System.AttributeTargets.Parameter | System.AttributeTargets.Property, AllowMultiple=false)]
public class DataTypeAttribute : System.ComponentModel.DataAnnotations.ValidationAttribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false)]
public class DataTypeAttribute : System.ComponentModel.DataAnnotations.ValidationAttribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Method | System.AttributeTargets.Parameter | System.AttributeTargets.Property, AllowMultiple=false)>]
type DataTypeAttribute = class
    inherit ValidationAttribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false)>]
type DataTypeAttribute = class
    inherit ValidationAttribute
Public Class DataTypeAttribute
Inherits ValidationAttribute
继承
派生
属性

示例

以下示例使用 DataTypeAttribute 特性自定义 EmailAddress 数据字段的显示。 电子邮件地址显示为超链接,而不是简单文本,动态数据将从内部数据类型推断出来。 示例代码分为三个部分,并执行以下步骤:

  • 它实现元数据分部类和关联的元数据类。

  • 在关联的元数据类中,它通过指定EmailAddress枚举值将DataTypeAttribute属性应用于 EmailAddress 数据字段。 这向 Text.ascx 字段模板指示它应自定义电子邮件地址的显示。

  • 它修改 Text.ascx 字段模板以自定义 EmailAddress 数据字段的显示。

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

[MetadataType(typeof(CustomerMetaData))]
public partial class Customer
{
}

public class CustomerMetaData
{

    // Add type information.
    [DataType(DataType.EmailAddress)]
    public object EmailAddress;
}
Imports System.Web.DynamicData
Imports System.ComponentModel.DataAnnotations

<MetadataType(GetType(CustomerMetadata))> _
Partial Public Class Customer


End Class

Public Class CustomerMetadata

    ' Add type information.
    <DataType(DataType.EmailAddress)> _
    Public EmailAddress As Object

End Class

<%@ Control Language="C#" 
CodeFile="Text.ascx.cs" Inherits="TextField" %>

<!-- Removed, evaluated in the code behind.
<%# FieldValueString %> -->
<%@ Control Language="VB" 
  CodeFile="Text.ascx.vb" Inherits="TextField" %>

<!-- Removed, evaluated in the code behind.
<%# FieldValueString %> -->
using System;
using System.Linq;
using System.Web.UI.WebControls;
using System.Web.DynamicData;
using System.ComponentModel.DataAnnotations;

public partial class TextField : 
    System.Web.DynamicData.FieldTemplateUserControl {

    protected override void OnDataBinding(EventArgs e)
    {
        base.OnDataBinding(e);
        bool processed = false;
        var metadata = MetadataAttributes.OfType
            <DataTypeAttribute>().FirstOrDefault();
        if (metadata != null)
        {
            if (metadata.DataType == DataType.EmailAddress)
            {
                if (!string.IsNullOrEmpty(FieldValueString))
                {
                    processed = true;
                    HyperLink hyperlink = new HyperLink();
                    hyperlink.Text = FieldValueString;
                    hyperlink.NavigateUrl = "mailto:" + FieldValueString;
                    Controls.Add(hyperlink);
                }
            }
        }
        if (!processed)
        {
            Literal literal = new Literal();
            literal.Text = FieldValueString;
            Controls.Add(literal);
        }
    }
}
Imports System.Linq
Imports System.Web.UI.WebControls
Imports System.Web.DynamicData
Imports System.ComponentModel.DataAnnotations

Partial Public Class TextField
    Inherits System.Web.DynamicData.FieldTemplateUserControl

    Protected Overloads Overrides Sub OnDataBinding(ByVal e As EventArgs)
        MyBase.OnDataBinding(e)
        Dim processed As Boolean = False
        Dim metadata As DataTypeAttribute = _
           MetadataAttributes.OfType(Of DataTypeAttribute)().FirstOrDefault()
        If metadata IsNot Nothing Then
            If metadata.DataType = DataType.EmailAddress Then
                If Not String.IsNullOrEmpty(FieldValueString) Then
                    processed = True
                    Dim hyperlink As New HyperLink()
                    hyperlink.Text = FieldValueString
                    hyperlink.NavigateUrl = "mailto:" + FieldValueString
                    Controls.Add(hyperlink)
                End If
            End If
        End If
        If Not processed Then
            Dim literal As New Literal()
            literal.Text = FieldValueString
            Controls.Add(literal)
        End If
    End Sub

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

注解

通过此属性 DataTypeAttribute ,可以使用比数据库内部类型更具体的类型来标记字段。 类型名称是从枚举类型中选择的 DataType 。 例如,可以将包含电子邮件地址的字符串数据字段指定为 EmailAddress 类型。 然后,字段模板会访问此信息,以修改数据字段的处理方式。

出于以下原因,请使用 DataTypeAttribute 该属性:

  • 为数据字段提供其他类型信息。 为此, DataTypeAttribute 可将属性应用于数据模型中的数据字段,并通过从 DataType 枚举中指定其他类型名称。 处理数据字段的字段模板可以访问此附加的元数据类型信息,以确定如何处理该字段。 例如,文本字段模板可以为其内部类型为 String的电子邮件地址生成超链接。

  • 将自定义字段模板与数据字段相关联。 然后,将使用指定的自定义字段模板来处理数据字段。 这是使用特性的 UIHintAttribute 替代方法。

将属性应用于 DataTypeAttribute 数据字段时,必须执行以下操作:

  • 遵循属性使用规则。

  • 实现包含要向其应用属性的数据字段的元数据类。

  • 适当地发出验证错误。

构造函数

名称 说明
DataTypeAttribute(DataType)

使用指定的类型名称初始化类的新实例 DataTypeAttribute

DataTypeAttribute(String)

使用指定的字段模板名称初始化类的新实例 DataTypeAttribute

属性

名称 说明
CustomDataType

获取与数据字段关联的自定义字段模板的名称。

DataType

获取与数据字段关联的类型。

DisplayFormat

获取数据字段显示格式。

ErrorMessage

获取或设置一条错误消息,以在验证失败时与验证控件相关联。

(继承自 ValidationAttribute)
ErrorMessageResourceName

获取或设置要使用的错误消息资源名称,以便在验证失败时查找 ErrorMessageResourceType 属性值。

(继承自 ValidationAttribute)
ErrorMessageResourceType

获取或设置在验证失败时用于错误消息查找的资源类型。

(继承自 ValidationAttribute)
ErrorMessageString

获取本地化的验证错误消息。

(继承自 ValidationAttribute)
RequiresValidationContext

获取一个值,该值指示属性是否需要验证上下文。

(继承自 ValidationAttribute)
TypeId

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

(继承自 Attribute)

方法

名称 说明
Equals(Object)

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

(继承自 Attribute)
FormatErrorMessage(String)

根据发生错误的数据字段,将格式应用于错误消息。

(继承自 ValidationAttribute)
GetDataTypeName()

返回与数据字段关联的类型的名称。

GetHashCode()

返回此实例的哈希代码。

(继承自 Attribute)
GetType()

获取当前实例的 Type

(继承自 Object)
GetValidationResult(Object, ValidationContext)

检查指定的值是否对当前验证属性有效。

(继承自 ValidationAttribute)
IsDefaultAttribute()

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

(继承自 Attribute)
IsValid(Object, ValidationContext)

验证与当前验证属性相关的指定值。

(继承自 ValidationAttribute)
IsValid(Object)

检查数据字段的值是否有效。

Match(Object)

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

(继承自 Attribute)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
ToString()

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

(继承自 Object)
Validate(Object, String)

验证指定的对象。

(继承自 ValidationAttribute)
Validate(Object, ValidationContext)

验证指定的对象。

(继承自 ValidationAttribute)

显式接口实现

名称 说明
_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)

适用于

另请参阅