DisplayFormatAttribute クラス

定義

ASP.NET Dynamic Data によるデータ フィールドの表示方法と書式を指定します。

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

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

注釈

この属性をデータ フィールドに適用する場合は、属性の使用に関するガイドラインに従う必要があります。 詳細については、「 ASP.NET 動的データ ガイドライン」を参照してください。

コンストラクター

DisplayFormatAttribute()

DisplayFormatAttribute クラスの新しいインスタンスを初期化します。

プロパティ

ApplyFormatInEditMode

データ フィールドが編集モードである場合に DataFormatString プロパティで指定した書式指定文字列をフィールド値に適用するかどうかを示す値を取得または設定します。

ConvertEmptyStringToNull

データ ソースのデータ フィールドを更新するときに、空の文字列値 ("") を null に自動的に変換するかどうかを示す値を取得または設定します。

DataFormatString

フィールド値の表示形式を取得または設定します。

HtmlEncode

フィールドを HTML エンコードするかどうかを示す値を取得または設定します。

NullDisplayText

フィールドの値が null の場合にフィールドに表示するテキストを取得または設定します。

NullDisplayTextResourceType

NullDisplayText のリソースを含む Type を取得または設定します。

NullDisplayTextNullDisplayTextResourceType を使用して、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)

適用対象

こちらもご覧ください