DataBinder クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
データ バインディング式構文を生成および解析するための迅速なアプリケーション開発 (RAD) デザイナーのサポートを提供します。 このクラスは継承できません。
public ref class DataBinder sealed
public sealed class DataBinder
type DataBinder = class
Public NotInheritable Class DataBinder
- 継承
-
DataBinder
例
次の例では、静的GetPropertyValue メソッドを使用して、Product オブジェクトのArrayListを使用して、Repeater コントロールのフィールドを設定します。
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 の詳細については、「Binding to Databases および Data-Binding Expressions Overview」を参照してください。
.NET Framework 4.5 以降では、モデル バインドを使用して、以前のバージョンのデータ バインディングを使用して実行する必要があったタスクの一部を簡略化できます。 Web フォームでのモデル バインドの使用に関するチュートリアル シリーズについては、「 モデル バインドと Web フォーム」を参照してください。
コンストラクター
| 名前 | 説明 |
|---|---|
| DataBinder() |
DataBinder クラスの新しいインスタンスを初期化します。 |
プロパティ
| 名前 | 説明 |
|---|---|
| EnableCaching |
実行時にデータ キャッシュが有効かどうかを示す値を取得または設定します。 |
メソッド
| 名前 | 説明 |
|---|---|
| Equals(Object) |
指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。 (継承元 Object) |
| Eval(Object, String, String) |
実行時にデータ バインディング式を評価し、結果を文字列として書式設定します。 |
| Eval(Object, String) |
実行時にデータ バインディング式を評価します。 |
| GetDataItem(Object, Boolean) |
成功または失敗を示すオブジェクトの宣言されたデータ項目を取得します。 |
| GetDataItem(Object) |
オブジェクトの宣言されたデータ項目を取得します。 |
| GetHashCode() |
既定のハッシュ関数として機能します。 (継承元 Object) |
| GetIndexedPropertyValue(Object, String, String) |
指定したコンテナーの指定したプロパティの値を取得し、結果を書式設定します。 |
| GetIndexedPropertyValue(Object, String) |
指定したコンテナーとナビゲーション パスのプロパティの値を取得します。 |
| GetPropertyValue(Object, String, String) |
指定したオブジェクトの指定したプロパティの値を取得し、結果を書式設定します。 |
| GetPropertyValue(Object, String) |
指定したオブジェクトの指定したプロパティの値を取得します。 |
| GetType() |
現在のインスタンスの Type を取得します。 (継承元 Object) |
| IsBindableType(Type) |
指定したデータ型をバインドできるかどうかを判断します。 |
| MemberwiseClone() |
現在の Objectの簡易コピーを作成します。 (継承元 Object) |
| ToString() |
現在のオブジェクトを表す文字列を返します。 (継承元 Object) |