データ バインディング式の構文
更新 : 2007 年 11 月
データ バインド式を使用すると、ページで DataBind メソッドが呼び出されたときに、サーバー コントロール プロパティとデータ ソースの間にバインディングが作成されます。データ バインド式は、サーバー コントロールの開始タグ内またはページの任意の場所で、属性と値のペアの右辺に記述できます。
<tagprefix:tagname property="<%# data-binding expression %>"
runat="server" />
- or -
literal text <%# data-binding expression %>
パラメータ
property
データ バインディングを宣言する対象となるコントロール プロパティ。データ バインド式
解説のセクションで概説する要件を満たす任意の式。
解説
すべてのデータ バインド式は、<%# 文字と %> 文字で囲む必要があります。
ASP.NET では、サーバー コントロール プロパティとデータ ソースの間にバインディングが作成される階層データ バインディング モデルがサポートされます。ほとんどのサーバー コントロール プロパティは、そのコンテナ ページ、またはサーバー コントロールの直接の名前付けコンテナのすべてのパブリック フィールドまたはプロパティに対してバインドできます。
データ バインド式は、Eval および Bind メソッドを使用してデータをコントロールにバインドし、変更内容をデータベースに返送します。Eval メソッドは、データ フィールドの値を取得して、文字列として返す静的な (読み取り専用) メソッドです。Bind メソッドは、読み取り/書き込み機能をサポートしており、データ バインド コントロールの値を取得して、すべての変更内容をデータベースに返送できます。
XPath メソッドと XPathSelect メソッドに加えて XPathBinder クラス使用して、XmlDataSource コントロールから XML データにバインドできます。詳細については、「XmlDataSource Web サーバー コントロールの概要」を参照してください。
使用例
ASP.NET サーバー コントロールのプロパティに対してデータ バインディングを実行するコード例を次に示します。ユーザーが DropDownList という Web サーバー コントロールから状態を選択すると、Label という Web サーバー コントロールがリスト内の選択された項目にバインドされ、選択された状態が表示されます。
<html>
<head>
<script language="C#" runat="server">
void SubmitBtn_Click(Object sender, EventArgs e) {
// Rather than explictly pulling out the variable from the StateList control
// and then manipulating a Label control, just call Page.DataBind.
// This will evaluate any <%# %> expressions within the page.
Page.DataBind();
}
</script>
</head>
<body>
<h3><font face="Verdana">Binding to a property of another server control</font></h3>
<form runat="server">
<asp:DropDownList id="StateList" runat="server">
<asp:ListItem>CA</asp:ListItem>
<asp:ListItem>IN</asp:ListItem>
<asp:ListItem>KS</asp:ListItem>
<asp:ListItem>MD</asp:ListItem>
<asp:ListItem>MI</asp:ListItem>
<asp:ListItem>OR</asp:ListItem>
<asp:ListItem>TN</asp:ListItem>
<asp:ListItem>UT</asp:ListItem>
</asp:DropDownList>
<asp:button Text="Submit" OnClick="SubmitBtn_Click" runat="server"/>
<p>
Selected State: <asp:label text='<%# StateList.SelectedItem.Text %>' runat="server"/>
</form>
</body>
</html>
<html>
<head>
<script language="VB" runat="server">
Sub SubmitBtn_Click(sender As Object, e As EventArgs)
' Rather than explictly pulling out the variable from the StateList control
' and then manipulating a Label control, just call Page.DataBind.
' This will evaluate any <%# %> expressions within the page.
Page.DataBind()
End Sub
</script>
</head>
<body>
<h3><font face="Verdana"> Binding to a property of another server control</font></h3>
<form runat="server">
<asp:DropDownList id="StateList" runat="server">
<asp:ListItem>CA</asp:ListItem>
<asp:ListItem>IN</asp:ListItem>
<asp:ListItem>KS</asp:ListItem>
<asp:ListItem>MD</asp:ListItem>
<asp:ListItem>MI</asp:ListItem>
<asp:ListItem>OR</asp:ListItem>
<asp:ListItem>TN</asp:ListItem>
<asp:ListItem>UT</asp:ListItem>
</asp:DropDownList>
<asp:button Text="Submit" OnClick="SubmitBtn_Click" runat="server"/>
<p>
Selected State: <asp:label text='<%# StateList.SelectedItem.Text %>' runat="server"/>
</form>
</body>
</html>
参照
概念
XmlDataSource Web サーバー コントロールの概要