TemplateField.ItemTemplate プロパティ
アセンブリ: System.Web (system.web.dll 内)

<TemplateContainerAttribute(GetType(IDataItemContainer), BindingDirection.TwoWay)> _ Public Overridable Property ItemTemplate As ITemplate
Dim instance As TemplateField Dim value As ITemplate value = instance.ItemTemplate instance.ItemTemplate = value
[TemplateContainerAttribute(typeof(IDataItemContainer), BindingDirection.TwoWay)] public virtual ITemplate ItemTemplate { get; set; }
[TemplateContainerAttribute(typeof(IDataItemContainer), BindingDirection::TwoWay)] public: virtual property ITemplate^ ItemTemplate { ITemplate^ get (); void set (ITemplate^ value); }
/** @property */ public ITemplate get_ItemTemplate () /** @property */ public void set_ItemTemplate (ITemplate value)
public function get ItemTemplate () : ITemplate public function set ItemTemplate (value : ITemplate)
TemplateField の項目を表示するときに使用するテンプレートを格納している System.Web.UI.ITemplate 実装オブジェクト。既定値は null 参照 (Visual Basic では Nothing) です。このプロパティが設定されていないことを示します。

ItemTemplate プロパティを使用して、TemplateField オブジェクトの項目に表示するカスタムの内容を指定します。項目を表示する方法を指定するテンプレートを作成して、内容を定義します。
![]() |
---|
オプションで AlternatingItemTemplate プロパティを ItemTemplate プロパティと組み合わせて定義して、データ バインド コントロールの項目を 1 つおきに異なる外観に設定できます。 |
テンプレートを指定するには、まず、<TemplateField> 要素の開始タグと終了タグの間に <ItemTemplate> の開始タグと終了タグを配置します。次に、開始 <ItemTemplate> タグと終了 <ItemTemplate> タグの間にカスタムの内容を追加します。内容は、簡単なプレーンテキストとしたり、テンプレートに他のコントロールを埋め込んで複雑にしたりできます。
テンプレートで定義されたコントロールにプログラムからアクセスするには、まず、そのコントロールが、データ バインド コントロールの、どの TableCell オブジェクトに含まれるかを確認します。次に、TableCell オブジェクトの Controls コレクションを使用してコントロールにアクセスします。コントロールに ID プロパティが指定されている場合は、TableCell オブジェクトの FindControl メソッドを使用してコントロールを検索することもできます。

ItemTemplate プロパティを使用して、GridView コントロールの TemplateField フィールドに表示される項目のカスタム テンプレートを作成するコード例を次に示します。このテンプレートは、RadioButtonList コントロールにフィールドの値を表示します。
<%@ Page language="VB" %> <script runat="server"> Sub TitleGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) ' Get the RadioButtonList control from the row. Dim radio As RadioButtonList = CType(e.Row.FindControl("TypeList"), RadioButtonList) ' Select the appropriate option button based on the value ' of the Type field for the row. In this example, the Type ' field values are stored in the hidden column in the ' GridView control. If Not radio Is Nothing Then Select Case e.Row.Cells(3).Text.Trim() Case "business" radio.SelectedIndex = 0 Case "mod_cook" radio.SelectedIndex = 1 Case "popular_comp" radio.SelectedIndex = 2 Case "psychology" radio.SelectedIndex = 3 Case "trad_cook" radio.SelectedIndex = 4 Case Else radio.SelectedIndex = 5 End Select End If End Sub </script> <html> <body> <form runat="server"> <h3>TemplateField ItemTemplate Example</h3> <!-- Populate the Columns collection declaratively. --> <!-- Create a custom TemplateField column that uses --> <!-- two Label controls to display an author's first and --> <!-- last name in the same column. --> <asp:gridview id="TitleGridView" datasourceid="TitleSqlDataSource" autogeneratecolumns="false" onrowdatabound="TitleGridView_RowDataBound" runat="server"> <columns> <asp:boundfield datafield="title" headertext="Title"/> <asp:boundfield datafield="price" dataformatstring="{0:c}" headertext="Price"/> <asp:templatefield headertext="Type"> <itemtemplate> <asp:radiobuttonlist id="TypeList" datasourceid="TypeSqlDataSource" datatextfield="type" enabled="false" runat="server"/> </itemtemplate> </asp:templatefield> <asp:boundfield datafield="type" visible="false"/> </columns> </asp:gridview> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Pubs sample database. --> <asp:sqldatasource id="TitleSqlDataSource" selectcommand="SELECT [title], [price], [type] FROM [titles]" connectionstring="server=localhost;database=pubs;integrated security=SSPI" runat="server"> </asp:sqldatasource> <asp:sqldatasource id="TypeSqlDataSource" selectcommand="SELECT Distinct [type] FROM [titles]" connectionstring="server=localhost;database=pubs;integrated security=SSPI" runat="server"> </asp:sqldatasource> </form> </body> </html>
<%@ Page language="C#" %> <script runat="server"> void TitleGridView_RowDataBound (Object sender, GridViewRowEventArgs e) { // Get the RadioButtonList control from the row. RadioButtonList radio = (RadioButtonList)e.Row.FindControl("TypeList"); // Select the appropriate option button based on the value // of the Type field for the row. In this example, the Type // field values are stored in the hidden column in the // GridView control. if (radio != null) { switch (e.Row.Cells[3].Text.Trim()) { case "business": radio.SelectedIndex = 0; break; case "mod_cook": radio.SelectedIndex = 1; break; case "popular_comp": radio.SelectedIndex = 2; break; case "psychology": radio.SelectedIndex = 3; break; case "trad_cook": radio.SelectedIndex = 4; break; default: radio.SelectedIndex = 5; break; } } } </script> <html> <body> <form runat="server"> <h3>TemplateField ItemTemplate Example</h3> <!-- Populate the Columns collection declaratively. --> <!-- Create a custom TemplateField column that uses --> <!-- two Label controls to display an author's first and --> <!-- last name in the same column. --> <asp:gridview id="TitleGridView" datasourceid="TitleSqlDataSource" autogeneratecolumns="false" onrowdatabound="TitleGridView_RowDataBound" runat="server"> <columns> <asp:boundfield datafield="title" headertext="Title"/> <asp:boundfield datafield="price" dataformatstring="{0:c}" headertext="Price"/> <asp:templatefield headertext="Type"> <itemtemplate> <asp:radiobuttonlist id="TypeList" datasourceid="TypeSqlDataSource" datatextfield="type" enabled="false" runat="server"/> </itemtemplate> </asp:templatefield> <asp:boundfield datafield="type" visible="false"/> </columns> </asp:gridview> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Pubs sample database. --> <asp:sqldatasource id="TitleSqlDataSource" selectcommand="SELECT [title], [price], [type] FROM [titles]" connectionstring="server=localhost;database=pubs;integrated security=SSPI" runat="server"> </asp:sqldatasource> <asp:sqldatasource id="TypeSqlDataSource" selectcommand="SELECT Distinct [type] FROM [titles]" connectionstring="server=localhost;database=pubs;integrated security=SSPI" runat="server"> </asp:sqldatasource> </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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- TemplateField.ItemTemplate プロパティのページへのリンク