DisplayFormatAttribute クラス

定義

動的データでデータ フィールドを表示および書式設定する方法 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 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 サンプル データベースをダウンロードしてインストールする方法については、GitHubの「Microsoft SQL Server 製品サンプル: データベースを参照してください。 実行しているSQL Serverのバージョンに合った正しいバージョンのサンプル データベースがインストールされていることを確認します。
  • データ ドリブン Web サイト。 これにより、カスタマイズするデータ フィールドを含むデータベースとクラスのデータ コンテキストを作成できます。 詳細については、Walkthrough: Creating a New Dynamic Data Web Site using Scaffoldingを参照してください。

注釈

この属性をデータ フィールドに適用する場合は、属性の使用に関するガイドラインに従う必要があります。

コンストラクター

名前 説明
DisplayFormatAttribute()

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

プロパティ

名前 説明
ApplyFormatInEditMode

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

ConvertEmptyStringToNull

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

DataFormatString

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

HtmlEncode

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

NullDisplayText

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

NullDisplayTextResourceType

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

NullDisplayTextResourceTypeNullDisplayTextと共に使用すると、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)

適用対象