DataBinder クラス

定義

Rapid Application Development (RAD) デザイナーのサポートを提供し、データ バインディング式の構文を生成および解析します。 このクラスは継承できません。

public ref class DataBinder sealed
public sealed class DataBinder
type DataBinder = class
Public NotInheritable Class DataBinder
継承
DataBinder

次の例では、 静的GetPropertyValueメソッドを使用して、 オブジェクトの を使用してコントロールのRepeaterフィールドをArrayListProduct設定します。 メソッドは Eval 同じ構文で適用できますが、すぐには実行されません。

この例では、文字列Modelプロパティと数値UnitPriceプロパティを公開するカスタム Product クラスを使用します。

<%@ Page Language="C#" %>
<%@ Import Namespace="ASPSample" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void  Page_Load(object sender, EventArgs e)
{
        // Create and populate an ArrayList to store the products.
        ArrayList ProductList = new ArrayList();
        ProductList.Add(new Product("Standard", 99.95));
        ProductList.Add(new Product("Deluxe", 159.95));

        // Bind the array list to Repeater
        ListRepeater.DataSource = ProductList;
        ListRepeater.DataBind();
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>DataBinder Example</title>
</head>
<body>
<form id="Form2" runat="server">
<table>
<asp:Repeater id="ListRepeater" runat="server">
    <HeaderTemplate>
    <tr>
        <th style="width:50; text-align:left">Model</th>
        <th style="width:100; text-align:right">Unit Price</th>
    </tr>
    </HeaderTemplate>
    <ItemTemplate>
    <tr>
        <!-- Databind to the Product information using the DataBinder methods. 
             The Container.DataItem refers to the ArrayList object bound to 
             the ASP:Repeater in the Page Load event. -->
        <td>
            <%#DataBinder.GetPropertyValue(Container.DataItem, "Model")%>
        </td>
        <!-- Format the UnitPrice as currency. ({0:c}) -->
        <td style="text-align:right">
            <%#DataBinder.GetPropertyValue(Container.DataItem,
                         "UnitPrice", "{0:c}")%>
        </td>
    </tr>
    </ItemTemplate>
</asp:Repeater>
</table>
</form>
</body>
</html>
<%@ Page Language="vb" %>
<%@ Import Namespace="ASPSample" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Private Sub Page_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load

        ' Create and populate an ArrayList to store the products.
        Dim ProductList As New ArrayList
        ProductList.Add(New Product("Standard", 99.95))
        ProductList.Add(New Product("Deluxe", 159.95))

        ' Bind the array list to Repeater
        ListRepeater.DataSource = ProductList
        ListRepeater.DataBind()

    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>DataBinder Example</title>
</head>
<body>
<form id="Form2" runat="server">
<table>
<asp:Repeater id="ListRepeater" runat="server">
    <HeaderTemplate>
    <tr>
        <th style="width:50; text-align:left">Model</th>
        <th style="width:100; text-align:right">Unit Price</th>
    </tr>
    </HeaderTemplate>
    <ItemTemplate>
    <tr>
        <!-- Databind to the Product information using the DataBinder methods. 
             The Container.DataItem refers to the ArrayList object bound to 
             the ASP:Repeater in the Page Load event. -->
        <td>
            <%#DataBinder.GetPropertyValue(Container.DataItem, "Model")%>
        </td>
        <!-- Format the UnitPrice as currency. ({0:c}) -->
        <td style="text-align:right">
            <%#DataBinder.GetPropertyValue(Container.DataItem, _
                         "UnitPrice", "{0:c}")%>
        </td>
    </tr>
    </ItemTemplate>
</asp:Repeater>
</table>
</form>
</body>
</html>

次のコードはカスタム Product クラスです。 このコードは、Product.cs や Product.vb など、App_Code ディレクトリ内の別のクラス ファイルに含める必要があります。

namespace ASPSample
{

    public class Product
    {
        string _Model;
        double _UnitPrice;

        public Product(string Model, double UnitPrice)
        {
            _Model = Model;
            _UnitPrice = UnitPrice;
        }

        // The product Model.
        public string Model
        {
            get {return _Model;}
            set {_Model = value;}
        }
            
        // The price of the each product.
        public double UnitPrice
        {
            get {return _UnitPrice;}
            set {_UnitPrice = value;}
        }
    }
}
Namespace ASPSample

    Public Class Product
        Private _Model As String
        Private _UnitPrice As Double

        ' The product Model.
        Public Property Model() As String
            Get
                Return _Model
            End Get
            Set(ByVal Value As String)
                _Model = Value
            End Set
        End Property

        ' The price of the each product.
        Public Property UnitPrice() As Double
            Get
                Return _UnitPrice
            End Get
            Set(ByVal Value As Double)
                _UnitPrice = Value
            End Set
        End Property


        Public Sub New(ByVal Model As String, ByVal UnitPrice As Double)
            _Model = Model
            _UnitPrice = UnitPrice
        End Sub

    End Class

End Namespace

注釈

このクラスのオーバーロードされた静的 Eval メソッドは、ASP.NET Web ページのデータ バインディング構文で使用できます。 これにより、標準のデータ バインディングよりも扱いやすい構文が提供されます。 ただし、型の自動変換が提供されるため DataBinder.Eval 、パフォーマンスが低下する可能性があります。

データ バインディング、式、構文 ASP.NET 詳細については、「 データベースへのバインド 」および「 データ バインディング式の概要」を参照してください。

.NET Framework 4.5 以降では、モデル バインドを使用して、以前のバージョンのデータ バインディングを使用して実行する必要があったタスクの一部を簡略化できます。 Web Formsでのモデル バインドの使用に関するチュートリアル シリーズについては、「モデル バインドとWeb Forms」を参照してください。

コンストラクター

DataBinder()

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

プロパティ

EnableCaching

実行時にデータ キャッシュが有効かどうかを示す値を取得または設定します。

メソッド

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
Eval(Object, String)

実行時にデータ バインディング式を評価します。

Eval(Object, String, String)

実行時にデータ バインディング式を評価し、その結果を文字列として書式設定します。

GetDataItem(Object)

オブジェクトの宣言されたデータ項目を取得します。

GetDataItem(Object, Boolean)

オブジェクトの宣言されたデータ項目を取得し、成功したか失敗したかを示します。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetIndexedPropertyValue(Object, String)

指定したコンテナーとナビゲーション パスのプロパティの値を取得します。

GetIndexedPropertyValue(Object, String, String)

指定したコンテナーの、指定したプロパティの値を取得した後、結果を書式設定します。

GetPropertyValue(Object, String)

指定したオブジェクトの、指定したプロパティの値を取得します。

GetPropertyValue(Object, String, String)

指定したオブジェクトの、指定したプロパティの値を取得した後、結果を書式設定します。

GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
IsBindableType(Type)

指定したデータ型にバインドできるかどうかを判断します。

MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

こちらもご覧ください