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 サンプル データベースをダウンロードしてインストールする方法については、「Microsoft SQL Server製品サンプル: GitHubのデータベース」を参照してください。 実行しているSQL Serverのバージョンに合った正しいバージョンのサンプル データベースをインストールしてください。

  • データ ドリブン Web サイト。 これにより、データベースのデータ コンテキストを作成し、カスタマイズするデータ フィールドを含むクラスを作成できます。 詳細については、「Walkthrough: Creating a New Dynamic Data Web Site using Scaffolding」を参照してください。

注釈

この DataTypeAttribute 属性を使用すると、データベース組み込み型よりも具体的な型を使用してフィールドをマークできます。 型名は列挙型から DataType 選択されます。 たとえば、電子メール アドレスを含む文字列データ フィールドを型として EmailAddress 指定できます。 その後、この情報にフィールド テンプレートがアクセスして、データ フィールドの処理方法を変更します。

この属性は DataTypeAttribute 、次の理由で使用します。

  • データ フィールドの追加の型情報を提供します。 これを行うには、データ モデルのデータ フィールドに属性を適用 DataTypeAttribute し、列挙型から追加の型名を DataType 指定します。 データ フィールドを処理するフィールド テンプレートは、この追加のメタデータ型情報にアクセスして、フィールドの処理方法を決定できます。 たとえば、テキスト フィールド テンプレートでは、組み込み型 Stringの電子メール アドレスのハイパーリンクを生成できます。

  • ユーザー設定フィールド テンプレートをデータ フィールドに関連付けるには。 指定したユーザー設定フィールド テンプレートは、データ フィールドの処理に使用されます。 これは、属性を使用する代わりに使用します UIHintAttribute

データ フィールドに属性を DataTypeAttribute 適用する場合は、次の操作を行う必要があります。

  • 属性の使用規則に従います。

  • 属性を適用するデータ フィールドを含むメタデータ クラスを実装します。

  • 必要に応じて検証エラーを発行します。

詳細については、「ASP.NET 動的データ ガイドライン」を参照してください。

コンストラクター

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)

データ フィールドの値が有効かどうかをチェックします。

IsValid(Object, ValidationContext)

現在の検証属性に対して、指定した値を検証します。

(継承元 ValidationAttribute)
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)

適用対象

こちらもご覧ください