DataList.ItemTemplate プロパティ

定義

DataList コントロールの項目のテンプレートを取得または設定します。

public:
 virtual property System::Web::UI::ITemplate ^ ItemTemplate { System::Web::UI::ITemplate ^ get(); void set(System::Web::UI::ITemplate ^ value); };
[System.ComponentModel.Browsable(false)]
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
[System.Web.UI.TemplateContainer(typeof(System.Web.UI.WebControls.DataListItem))]
public virtual System.Web.UI.ITemplate ItemTemplate { get; set; }
[<System.ComponentModel.Browsable(false)>]
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
[<System.Web.UI.TemplateContainer(typeof(System.Web.UI.WebControls.DataListItem))>]
member this.ItemTemplate : System.Web.UI.ITemplate with get, set
Public Overridable Property ItemTemplate As ITemplate

プロパティ値

ITemplate

ITemplate コントロールの項目のテンプレートを格納している DataList。 既定値は null です。

属性

次のコード例は、プロパティを使用 ItemTemplate してコントロール内の項目の内容を制御する方法を DataList 示しています。

注意

次のコード サンプルでは、単一ファイルのコード モデルを使用します。分離コード ファイルに直接コピーすると、正しく動作しない場合があります。 このコード サンプルは、拡張子が .aspx の空のテキスト ファイルにコピーする必要があります。 Web Forms コード モデルの詳細については、「ASP.NET Web Forms ページ コード モデル」を参照してください。


<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
   <script runat="server">
 
      ICollection CreateDataSource() 
      {
      
         // Create sample data for the DataList control.
         DataTable dt = new DataTable();
         DataRow dr;
 
         // Define the columns of the table.
         dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
         dt.Columns.Add(new DataColumn("StringValue", typeof(String)));
         dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
         dt.Columns.Add(new DataColumn("ImageValue", typeof(String)));
 
         // Populate the table with sample values.
         for (int i = 0; i < 9; i++) 
         {
            dr = dt.NewRow();
 
            dr[0] = i;
            dr[1] = "Description for item " + i.ToString();
            dr[2] = 1.23 * (i + 1);
            dr[3] = "Image" + i.ToString() + ".jpg";
 
            dt.Rows.Add(dr);
         }
 
         DataView dv = new DataView(dt);
         return dv;
      }
 
 
      void Page_Load(Object sender, EventArgs e) 
      {

         // Load sample data only once, when the page is first loaded.
         if (!IsPostBack) 
         {
            ItemsList.DataSource = CreateDataSource();
            ItemsList.DataBind();
         }

      }
 
      void Button_Click(Object sender, EventArgs e) 
      {
 
         // Set the repeat direction based on the selected value of the
         // DirectionList DropDownList control.
         ItemsList.RepeatDirection = 
             (RepeatDirection)DirectionList.SelectedIndex;

         // Set the repeat layout based on the selected value of the
         // LayoutList DropDownList control.
         ItemsList.RepeatLayout = (RepeatLayout)LayoutList.SelectedIndex;

         // Set the number of columns to display based on the selected
         // value of the ColumnsList DropDownList control.
         ItemsList.RepeatColumns = ColumnsList.SelectedIndex;

         // Show or hide the gridlines based on the value of the
         // ShowBorderCheckBox property. Note that gridlines are displayed
         // only if the RepeatLayout property is set to Table.
         if ((ShowBorderCheckBox.Checked) 
             && (ItemsList.RepeatLayout == RepeatLayout.Table)) 
         {
            ItemsList.BorderWidth = Unit.Pixel(1);
            ItemsList.GridLines = GridLines.Both;
         }    
         else  
         {
            ItemsList.BorderWidth = Unit.Pixel(0);
            ItemsList.GridLines = GridLines.None;
         }
    
      }    
 
   </script>
 
<head runat="server">
    <title>DataList Example</title>
</head>
<body>
 
   <form id="form1" runat="server">

      <h3>DataList Example</h3>
 
      <asp:DataList id="ItemsList"
           BorderColor="black"
           CellPadding="5"
           CellSpacing="5"
           RepeatDirection="Vertical"
           RepeatLayout="Table"
           RepeatColumns="0"
           BorderWidth="0"
           runat="server">

         <HeaderStyle BackColor="#aaaadd">
         </HeaderStyle>

         <AlternatingItemStyle BackColor="Gainsboro">
         </AlternatingItemStyle>

         <HeaderTemplate>

            List of items

         </HeaderTemplate>
               
         <ItemTemplate>

            Description: <br />
            <%# DataBinder.Eval(Container.DataItem, "StringValue") %>

            <br />

            Price: <%# DataBinder.Eval(Container.DataItem, "CurrencyValue", "{0:c}") %>

            <br />

            <asp:Image id="ProductImage"
                 AlternatingText='<%# DataBinder.Eval(Container.DataItem, "StringValue") %>'
                 ImageUrl='<%# DataBinder.Eval(Container.DataItem, "ImageValue") %>'
                 runat="server"/>

         </ItemTemplate>
 
      </asp:DataList>
 
      <hr />

      <table cellpadding="5">

         <tr>

            <th>

               Repeat direction:

            </th>

            <th>

               Repeat layout:

            </th>

            <th>

               Repeat columns:

            </th>

            <th>

               <asp:CheckBox id="ShowBorderCheckBox"
                    Text="Show border"
                    Checked="False" 
                    runat="server" />

            </th>

         </tr>

         <tr>

            <td>

               <asp:DropDownList id="DirectionList" 
                    runat="server">

                  <asp:ListItem>Horizontal</asp:ListItem>
                  <asp:ListItem Selected="True">Vertical</asp:ListItem>

               </asp:DropDownList>

            </td>

            <td>

               <asp:DropDownList id="LayoutList" 
                    runat="server">

                  <asp:ListItem Selected="True">Table</asp:ListItem>
                  <asp:ListItem>Flow</asp:ListItem>

               </asp:DropDownList>

            </td>

            <td>

               <asp:DropDownList id="ColumnsList" 
                    runat="server">

                  <asp:ListItem Selected="True">0</asp:ListItem>
                  <asp:ListItem>1</asp:ListItem>
                  <asp:ListItem>2</asp:ListItem>
                  <asp:ListItem>3</asp:ListItem>
                  <asp:ListItem>4</asp:ListItem>
                  <asp:ListItem>5</asp:ListItem>

               </asp:DropDownList>

            </td>

            <td>

                

            </td>


         </tr>

      </table>     
         
      <asp:LinkButton id="RefreshButton" 
           Text="Refresh DataList" 
           OnClick="Button_Click" 
           runat="server"/>
 
   </form>
 
</body>
</html>

<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
   <script runat="server">
 
      Function CreateDataSource() As ICollection 
      
         ' Create sample data for the DataList control.
         Dim dt As DataTable = New DataTable()
         dim dr As DataRow
 
         ' Define the columns of the table.
         dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
         dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
         dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))
         dt.Columns.Add(New DataColumn("ImageValue", GetType(String)))
 
         ' Populate the table with sample values.
         Dim i As Integer

         For i = 0 To 8 

            dr = dt.NewRow()
 
            dr(0) = i
            dr(1) = "Description for item " & i.ToString()
            dr(2) = 1.23 * (i + 1)
            dr(3) = "Image" & i.ToString() & ".jpg"
 
            dt.Rows.Add(dr)

         Next i
 
         Dim dv As DataView = New DataView(dt)
         Return dv

      End Function
 
      Sub Page_Load(sender As Object, e As EventArgs) 

         ' Load sample data only once, when the page is first loaded.
         If Not IsPostBack Then 
     
            ItemsList.DataSource = CreateDataSource()
            ItemsList.DataBind()
         
         End If

      End Sub
 
      Sub Button_Click(sender As Object, e As EventArgs) 
 
         ' Set the repeat direction based on the selected value of the
         ' DirectionList DropDownList control.
         ItemsList.RepeatDirection = _
             CType(DirectionList.SelectedIndex, RepeatDirection)

         ' Set the repeat layout based on the selected value of the
         ' LayoutList DropDownList control.
         ItemsList.RepeatLayout = CType(LayoutList.SelectedIndex, RepeatLayout)

         ' Set the number of columns to display based on the selected
         ' value of the ColumnsList DropDownList control.
         ItemsList.RepeatColumns = ColumnsList.SelectedIndex

         ' Show or hide the gridlines based on the value of the
         ' ShowBorderCheckBox. Note that gridlines are displayed
         ' only if the RepeatLayout property is set to Table.
         If ShowBorderCheckBox.Checked _
             And ItemsList.RepeatLayout = RepeatLayout.Table Then 

            ItemsList.BorderWidth = Unit.Pixel(1)
            ItemsList.GridLines = GridLines.Both
         
         Else  
    
            ItemsList.BorderWidth = Unit.Pixel(0)
            ItemsList.GridLines = GridLines.None
         
         End If
    
      End Sub    
 
   </script>
 
<head runat="server">
    <title>DataList Example</title>
</head>
<body>
 
   <form id="form1" runat="server">

      <h3>DataList Example</h3>
 
      <asp:DataList id="ItemsList"
           BorderColor="black"
           CellPadding="5"
           CellSpacing="5"
           RepeatDirection="Vertical"
           RepeatLayout="Table"
           RepeatColumns="0"
           BorderWidth="0"
           runat="server">

         <HeaderStyle BackColor="#aaaadd">
         </HeaderStyle>

         <AlternatingItemStyle BackColor="Gainsboro">
         </AlternatingItemStyle>

         <HeaderTemplate>

            List of items

         </HeaderTemplate>
               
         <ItemTemplate>

            Description: <br />
            <%# DataBinder.Eval(Container.DataItem, "StringValue") %>

            <br />

            Price: <%# DataBinder.Eval(Container.DataItem, "CurrencyValue", "{0:c}") %>

            <br />

            <asp:Image id="ProductImage"
                 AlternatingText='<%# DataBinder.Eval(Container.DataItem, "StringValue") %>'
                 ImageUrl='<%# DataBinder.Eval(Container.DataItem, "ImageValue") %>'
                 runat="server"/>

         </ItemTemplate>
 
      </asp:DataList>
 
      <hr />

      <table cellpadding="5">

         <tr>

            <th>

               Repeat direction:

            </th>

            <th>

               Repeat layout:

            </th>

            <th>

               Repeat columns:

            </th>

            <th>

               <asp:CheckBox id="ShowBorderCheckBox"
                    Text="Show border"
                    Checked="False" 
                    runat="server" />

            </th>

         </tr>

         <tr>

            <td>

               <asp:DropDownList id="DirectionList" 
                    runat="server">

                  <asp:ListItem>Horizontal</asp:ListItem>
                  <asp:ListItem Selected="True">Vertical</asp:ListItem>

               </asp:DropDownList>

            </td>

            <td>

               <asp:DropDownList id="LayoutList" 
                    runat="server">

                  <asp:ListItem Selected="True">Table</asp:ListItem>
                  <asp:ListItem>Flow</asp:ListItem>

               </asp:DropDownList>

            </td>

            <td>

               <asp:DropDownList id="ColumnsList" 
                    runat="server">

                  <asp:ListItem Selected="True">0</asp:ListItem>
                  <asp:ListItem>1</asp:ListItem>
                  <asp:ListItem>2</asp:ListItem>
                  <asp:ListItem>3</asp:ListItem>
                  <asp:ListItem>4</asp:ListItem>
                  <asp:ListItem>5</asp:ListItem>

               </asp:DropDownList>

            </td>

            <td>

                

            </td>


         </tr>

      </table>     
         
      <asp:LinkButton id="RefreshButton" 
           Text="Refresh DataList" 
           OnClick="Button_Click" 
           runat="server"/>
 
   </form>
 
</body>
</html>

注釈

プロパティを ItemTemplate 使用して、コントロール内の項目の内容を DataList 制御します。 コントロール内 DataList の項目の外観は、プロパティによって ItemStyle 制御されます。

コントロールの項目のテンプレートを指定するには、コントロールのDataList<ItemTemplate>開始タグと終了タグの間にタグをDataList配置します。 その後、開始タグと終了 <ItemTemplate> タグの間のテンプレートの内容を一覧表示できます。

必要に応じて、このプロパティを AlternatingItemTemplate 使用して、コントロール内の交互の項目に異なる外観を DataList 提供できます。

注意事項

このコントロールは、悪意のあるクライアント スクリプトを含む可能性があるユーザー入力を表示するために使用できます。 実行可能スクリプト、SQL ステートメント、またはその他のコードについてクライアントから送信された情報を、アプリケーションに表示する前に確認します。 入力コントロールを使用すると、コントロールに入力テキストを表示する前に、ユーザー入力を確認できます。 ASP.NET は、ユーザー入力のスクリプトと HTML をブロックする入力要求検証機能を提供します。 詳細については、「標準コントロールのセキュリティ保護」、「方法: 文字列に HTML エンコードを適用して Web アプリケーションのスクリプトの悪用から保護する」および「ASP.NET Web ページでのユーザー入力の検証」を参照してください。

適用対象

こちらもご覧ください