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 データ フィールドの表示をカスタマイズします。 電子メール アドレスは、単純なテキストではなくハイパーリンクとして表示されます。これは、動的データが組み込みデータ型から推論する内容です。 コード例は 3 つの部分にあり、次の手順を実行します。

  • メタデータ部分クラスと関連付けられたメタデータ クラスを実装します。

  • 関連付けられたメタデータ クラスで、 DataTypeAttribute 属性を EmailAddress データ フィールドに適用するには、 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 製品サンプル: データベースを参照してください。 実行しているSQL Serverのバージョンに合った正しいバージョンのサンプル データベースがインストールされていることを確認します。

  • データ ドリブン Web サイト。 これにより、データベースのデータ コンテキストを作成し、カスタマイズするデータ フィールドを含むクラスを作成できます。 詳細については、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)

適用対象

こちらもご覧ください