IBindableTemplate インターフェイス
アセンブリ: System.Web (system.web.dll 内)


IBindableTemplate インターフェイスを使用すると、データ バインド コントロールにテンプレート コンテンツが含まれている場合に、DetailsView、GridView、FormView などの ASP.NET データ バインド コントロールを ObjectDataSource または SqlDataSource などの ASP.NET データ ソース コントロールで指定されたデータにバインドできます。
![]() |
---|
ページの開発者は IBindableTemplate インターフェイスを実装しません。カスタムのデータ バインド コントロールを作成している開発者が IBindableTemplate オブジェクトを PerformDataBinding メソッドおよび ExtractRowValues メソッドの実装で操作する場合がありますが、独自の IBindableTemplate オブジェクトは実装しません。 |
GridView などのデータ バインド コントロールでは、通常、子コントロールのツリーが認識されます。そのため、データ バインディングが行われるたびに、データ バインド コントロールに値をバインドしたり、データ バインド コントロールから値を抽出したりできます。また、データ バインド コントロールとデータ ソース コントロール間で値をやりとりできます。ただし、ページの開発者がデータ バインド コントロールのテンプレート コンテンツを定義すると、親データ バインド コントロールからはそのテンプレート内の子コントロールが認識されません。(コントロールがコントロール自体を効果的に描画するので) 親コントロールはその子コントロールのコンテンツを描画できますが、これらの子コントロールの値を抽出し、関連付けられているデータ ソース コントロールの更新、挿入、または削除操作に渡すことはできません。データ バインディングでは、テンプレート コンテンツは親のデータ バインド コントロールには不透明です。Bind 構文を使用すると、データ バインド コントロールを使用して、IBindableTemplate インスタンス内でデータがバインドされているコントロールから値を抽出できます。
データ バインディングは一方向または双方向で実行できます (これらのバインディングの方向は、BindingDirection 列挙体によって定義されます)。一方向のデータ バインディングは、データ ソース コントロールからデータ バインド コントロールへの送信方向に実行されるデータ バインディングです。たとえば、データの読み取りでは一方向のデータ バインディングが行われます。一方向のデータ バインディングでは、テンプレート コンテンツ内で一方向のデータ バインディング構文 (<%# Eval("fieldname") %>) を使用できるので、双方向の ASP.NET データ バインディング構文を使用する必要はありません。双方向のデータ バインディングは、データ バインド コントロールからデータ ソース コントロールへの着信方向のデータ バインディングを表します。ASP.NET のデータ バインド コントロールおよびデータ ソース コントロールを使用した自動的な編集、挿入、および削除は双方向のデータ バインディングです。このような場合は、双方向のデータ バインディング式 (<%# Bind("fieldname") %>) を使用します。IBindableTemplate インターフェイスおよび ASP.NET インフラストラクチャでは、ASP.NET データ ソース コントロールとテンプレート コンテンツの間で、自動的な、宣言による双方向のデータ バインディングをサポートしています。ASP.NET のデータ バインディング式およびデータ バインディング構文の詳細については、「データベースへのバインド」および「データ バインディング式の構文」を参照してください。
データ バインド コントロールのテンプレート コンテンツは、ほとんどの場合、宣言を使用して定義されます。次の表に、テンプレート データをデータ バインド コントロールにバインドするために使用する最も一般的なプロセスを示します。
DetailsView | データ バインド コントロールはデータ ソース コントロールの DataSourceID プロパティを使用してデータにバインドされ、テンプレート コンテンツは ItemTemplate、EditItemTemplate、または InsertItemTemplate の各プロパティで定義されます。 |
GridView | データ バインド コントロールはデータ ソース コントロールの DataSourceID プロパティを使用してデータにバインドされ、テンプレート コンテンツは ItemTemplate プロパティまたは EditItemTemplate プロパティで定義されます。GridView コントロールでは挿入操作はサポートされません。 |
FormView | データ バインド コントロールは DataSourceID プロパティを使用してデータにバインドされ、テンプレート コンテンツは ItemTemplate、InsertItemTemplate、EditItemTemplate の各プロパティ、または TemplateField オブジェクトで定義されます。 |
DataList コントロールおよび Repeater コントロールでは、自動的な双方向のデータ バインディングはサポートされません。
テンプレート内の ASP.NET データ ソース コントロールにバインドされるテンプレート コンテンツの解析時に、ASP.NET によって暗黙的に IBindableTemplate オブジェクトが作成されます。具体的には、ASP.NET データ バインディング構文を使用し、データ バインディングをサポートする ASP.NET Web サーバー コントロールを含むテンプレート コンテンツが ASP.NET パーサーにより解析されるときに、ASP.NET パーサーによって CompiledBindableTemplateBuilder クラスのインスタンスが作成されます。これらの ASP.NET サーバー コントロールは、BindableAttribute 属性でマークされます。
IBindableTemplate インターフェイスでは、1 つのメソッド ExtractValues が定義されます。このメソッドは、双方向のデータ バインディングに対して定義されるので、データ バインド コントロールにより自動的に名前と値のペアがテンプレート コンテンツから抽出されて、そのペアがデータ ソース コントロールに実行時に渡されます。自動的なデータ バインディングが成功するには、ExtractValues メソッドによって抽出されたテンプレート コンテンツのフィールド名が、関連付けられているデータ ソース コントロールのパラメータ名と一致する必要があります。コントロールの開発者は、ExtractValues メソッドを、ExtractRowValues メソッド、または他のカスタムのデータ バインド コントロールの類似メソッドを実装する場合に限り明示的に呼び出します。

FormView コントロールを使用してテンプレート コンテンツを宣言によって定義し、SqlDataSource コントロールで指定したデータにバインドし、既存のレコードを表示および編集する方法を次のコード例に示します。ASP.NET パーサーは、実行時にテンプレート コンテンツを解析し、IBindableTemplate オブジェクトを作成します。このオブジェクトは、一方向の ASP.NET データ バインディング構文 (<%# Eval("fieldname") %>) と双方向の ASP.NET データ バインディング構文 (<%# Bind("fieldname") %>) の両方によってテンプレートで定義されているデータ バインド領域に、SqlDataSource コントロールの値をバインドできます。
<%@ Page language="VB" %> <script runat="server"> Sub EmployeeFormView_ItemUpdating(ByVal sender As Object, ByVal e As FormViewUpdateEventArgs) Handles EmployeeFormView.ItemUpdating ' Validate the field values entered by the user. This ' example determines whether the user left any fields ' empty. Use the NewValues property to access the new ' values entered by the user. Dim emptyFieldList As ArrayList = ValidateFields(e.NewValues) If emptyFieldList.Count > 0 Then ' The user left some fields empty. Display an error message. ' Use the Keys property to retrieve the key field value. Dim keyValue As String = e.Keys("EmployeeID").ToString() MessageLabel.Text = "You must enter a value for each field of record " & _ keyValue & ".<br/>The following fields are missing:<br/><br/>" ' Display the missing fields. Dim value As String For Each value In emptyFieldList ' Use the OldValues property to access the original value ' of a field. MessageLabel.Text &= value & " - Original Value = " & _ e.OldValues(value).ToString() & "<br>" Next ' Cancel the update operation. e.Cancel = True Else ' The field values passed validation. Clear the ' error message label. MessageLabel.Text = "" End If End Sub Function ValidateFields(ByVal list As IOrderedDictionary) As ArrayList ' Create an ArrayList object to store the ' names of any empty fields. Dim emptyFieldList As New ArrayList() ' Iterate though the field values entered by ' the user and check for an empty field. Empty ' fields contain a null value. Dim entry As DictionaryEntry For Each entry In list If entry.Value Is String.Empty Then ' Add the field name to the ArrayList object. emptyFieldList.Add(entry.Key.ToString()) End If Next Return emptyFieldList End Function Sub EmployeeFormView_ModeChanging(ByVal sender As Object, ByVal e As FormViewModeEventArgs) Handles EmployeeFormView.ModeChanging If e.CancelingEdit Then ' The user canceled the update operation. ' Clear the error message label. MessageLabel.Text = "" End If End Sub </script> <html> <body> <form runat="server"> <h3>FormView Example</h3> <asp:formview id="EmployeeFormView" datasourceid="EmployeeSource" allowpaging="true" datakeynames="EmployeeID" headertext="Employee Record" emptydatatext="No employees found." runat="server"> <headerstyle backcolor="CornFlowerBlue" forecolor="White" font-size="14" horizontalalign="Center" wrap="false"/> <rowstyle backcolor="LightBlue" wrap="false"/> <pagerstyle backcolor="CornFlowerBlue"/> <itemtemplate> <table> <tr> <td rowspan="6"> <asp:image id="EmployeeImage" imageurl='<%# Eval("PhotoPath") %>' alternatetext='<%# Eval("LastName") %>' runat="server"/> </td> <td colspan="2"> </td> </tr> <tr> <td> <b>Name:</b> </td> <td> <%# Eval("FirstName") %> <%# Eval("LastName") %> </td> </tr> <tr> <td> <b>Title:</b> </td> <td> <%# Eval("Title") %> </td> </tr> <tr> <td> <b>Hire Date:</b> </td> <td> <%# Eval("HireDate","{0:d}") %> </td> </tr> <tr height="150" valign="top"> <td> <b>Address:</b> </td> <td> <%# Eval("Address") %><br/> <%# Eval("City") %> <%# Eval("Region") %> <%# Eval("PostalCode") %><br/> <%# Eval("Country") %> </td> </tr> <tr> <td colspan="2"> <asp:linkbutton id="Edit" text="Edit" commandname="Edit" runat="server"/> </td> </tr> </table> </itemtemplate> <edititemtemplate> <table> <tr> <td rowspan="6"> <asp:image id="EmployeeEditImage" imageurl='<%# Eval("PhotoPath") %>' alternatetext='<%# Eval("LastName") %>' runat="server"/> </td> <td colspan="2"> </td> </tr> <tr> <td> <b>Name:</b> </td> <td> <asp:textbox id="FirstNameUpdateTextBox" text='<%# Bind("FirstName") %>' runat="server"/> <asp:textbox id="LastNameUpdateTextBox" text='<%# Bind("LastName") %>' runat="server"/> </td> </tr> <tr> <td> <b>Title:</b> </td> <td> <asp:textbox id="TitleUpdateTextBox" text='<%# Bind("Title") %>' runat="server"/> </td> </tr> <tr> <td> <b>Hire Date:</b> </td> <td> <asp:textbox id="HireDateUpdateTextBox" text='<%# Bind("HireDate", "{0:d}") %>' runat="server"/> </td> </tr> <tr height="150" valign="top"> <td> <b>Address:</b> </td> <td> <asp:textbox id="AddressUpdateTextBox" text='<%# Bind("Address") %>' runat="server"/> <br/> <asp:textbox id="CityUpdateTextBox" text='<%# Bind("City") %>' runat="server"/> <asp:textbox id="RegionUpdateTextBox" text='<%# Bind("Region") %>' width="40" runat="server"/> <asp:textbox id="PostalCodeUpdateTextBox" text='<%# Bind("PostalCode") %>' width="60" runat="server"/> <br/> <asp:textbox id="CountryUpdateTextBox" text='<%# Bind("Country") %>' runat="server"/> </td> </tr> <tr> <td colspan="2"> <asp:linkbutton id="UpdateButton" text="Update" commandname="Update" runat="server"/> <asp:linkbutton id="CancelButton" text="Cancel" commandname="Cancel" runat="server"/> </td> </tr> </table> </edititemtemplate> <pagersettings position="Bottom" mode="Numeric"/> </asp:formview> <br/><br/> <asp:label id="MessageLabel" forecolor="Red" runat="server"/> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the Web.config file. --> <asp:sqldatasource id="EmployeeSource" selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [Address], [City], [Region], [PostalCode], [Country], [HireDate], [PhotoPath] From [Employees]" updatecommand="Update [Employees] Set [LastName]=@LastName, [FirstName]=@FirstName, [Title]=@Title, [Address]=@Address, [City]=@City, [Region]=@Region, [PostalCode]=@PostalCode, [Country]=@Country Where [EmployeeID]=@EmployeeID" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>
<%@ Page language="C#" %> <script runat="server"> void EmployeeFormView_ItemUpdating(Object sender, FormViewUpdateEventArgs e) { // Validate the field values entered by the user. This // example determines whether the user left any fields // empty. Use the NewValues property to access the new // values entered by the user. ArrayList emptyFieldList = ValidateFields(e.NewValues); if (emptyFieldList.Count > 0) { // The user left some fields empty. Display an error message. // Use the Keys property to retrieve the key field value. String keyValue = e.Keys["EmployeeID"].ToString(); MessageLabel.Text = "You must enter a value for each field of record " + keyValue + ".<br/>The following fields are missing:<br/><br/>"; // Display the missing fields. foreach (String value in emptyFieldList) { // Use the OldValues property to access the original value // of a field. MessageLabel.Text += value + " - Original Value = " + e.OldValues[value].ToString() + "<br>"; } // Cancel the update operation. e.Cancel = true; } else { // The field values passed validation. Clear the // error message label. MessageLabel.Text = ""; } } ArrayList ValidateFields(IOrderedDictionary list) { // Create an ArrayList object to store the // names of any empty fields. ArrayList emptyFieldList = new ArrayList(); // Iterate though the field values entered by // the user and check for an empty field. Empty // fields contain a null value. foreach (DictionaryEntry entry in list) { if (entry.Value == String.Empty) { // Add the field name to the ArrayList object. emptyFieldList.Add(entry.Key.ToString()); } } return emptyFieldList; } void EmployeeFormView_ModeChanging(Object sender, FormViewModeEventArgs e) { if (e.CancelingEdit) { // The user canceled the update operation. // Clear the error message label. MessageLabel.Text = ""; } } </script> <html> <body> <form runat="server"> <h3>FormView Example</h3> <asp:formview id="EmployeeFormView" datasourceid="EmployeeSource" allowpaging="true" datakeynames="EmployeeID" headertext="Employee Record" emptydatatext="No employees found." onitemupdating="EmployeeFormView_ItemUpdating" onmodechanging="EmployeeFormView_ModeChanging" runat="server"> <headerstyle backcolor="CornFlowerBlue" forecolor="White" font-size="14" horizontalalign="Center" wrap="false"/> <rowstyle backcolor="LightBlue" wrap="false"/> <pagerstyle backcolor="CornFlowerBlue"/> <itemtemplate> <table> <tr> <td rowspan="6"> <asp:image id="EmployeeImage" imageurl='<%# Eval("PhotoPath") %>' alternatetext='<%# Eval("LastName") %>' runat="server"/> </td> <td colspan="2"> </td> </tr> <tr> <td> <b>Name:</b> </td> <td> <%# Eval("FirstName") %> <%# Eval("LastName") %> </td> </tr> <tr> <td> <b>Title:</b> </td> <td> <%# Eval("Title") %> </td> </tr> <tr> <td> <b>Hire Date:</b> </td> <td> <%# Eval("HireDate","{0:d}") %> </td> </tr> <tr height="150" valign="top"> <td> <b>Address:</b> </td> <td> <%# Eval("Address") %><br/> <%# Eval("City") %> <%# Eval("Region") %> <%# Eval("PostalCode") %><br/> <%# Eval("Country") %> </td> </tr> <tr> <td colspan="2"> <asp:linkbutton id="Edit" text="Edit" commandname="Edit" runat="server"/> </td> </tr> </table> </itemtemplate> <edititemtemplate> <table> <tr> <td rowspan="6"> <asp:image id="EmployeeEditImage" imageurl='<%# Eval("PhotoPath") %>' alternatetext='<%# Eval("LastName") %>' runat="server"/> </td> <td colspan="2"> </td> </tr> <tr> <td> <b>Name:</b> </td> <td> <asp:textbox id="FirstNameUpdateTextBox" text='<%# Bind("FirstName") %>' runat="server"/> <asp:textbox id="LastNameUpdateTextBox" text='<%# Bind("LastName") %>' runat="server"/> </td> </tr> <tr> <td> <b>Title:</b> </td> <td> <asp:textbox id="TitleUpdateTextBox" text='<%# Bind("Title") %>' runat="server"/> </td> </tr> <tr> <td> <b>Hire Date:</b> </td> <td> <asp:textbox id="HireDateUpdateTextBox" text='<%# Bind("HireDate", "{0:d}") %>' runat="server"/> </td> </tr> <tr height="150" valign="top"> <td> <b>Address:</b> </td> <td> <asp:textbox id="AddressUpdateTextBox" text='<%# Bind("Address") %>' runat="server"/> <br/> <asp:textbox id="CityUpdateTextBox" text='<%# Bind("City") %>' runat="server"/> <asp:textbox id="RegionUpdateTextBox" text='<%# Bind("Region") %>' width="40" runat="server"/> <asp:textbox id="PostalCodeUpdateTextBox" text='<%# Bind("PostalCode") %>' width="60" runat="server"/> <br/> <asp:textbox id="CountryUpdateTextBox" text='<%# Bind("Country") %>' runat="server"/> </td> </tr> <tr> <td colspan="2"> <asp:linkbutton id="UpdateButton" text="Update" commandname="Update" runat="server"/> <asp:linkbutton id="CancelButton" text="Cancel" commandname="Cancel" runat="server"/> </td> </tr> </table> </edititemtemplate> <pagersettings position="Bottom" mode="Numeric"/> </asp:formview> <br/><br/> <asp:label id="MessageLabel" forecolor="Red" runat="server"/> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the Web.config file. --> <asp:sqldatasource id="EmployeeSource" selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [Address], [City], [Region], [PostalCode], [Country], [HireDate], [PhotoPath] From [Employees]" updatecommand="Update [Employees] Set [LastName]=@LastName, [FirstName]=@FirstName, [Title]=@Title, [Address]=@Address, [City]=@City, [Region]=@Region, [PostalCode]=@PostalCode, [Country]=@Country Where [EmployeeID]=@EmployeeID" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


IBindableTemplate メソッド

名前 | 説明 | |
---|---|---|
![]() | ExtractValues | クラスで実装された場合、テンプレート コンテンツ内で双方向の ASP.NET データ バインディング構文を使用してバインドされている値の、一連の名前と値のペアを取得します。 |

IBindableTemplate メンバ
- IBindableTemplateのページへのリンク