Repeater.CreateItem(Int32, ListItemType) Method

Definition

Creates a RepeaterItem object with the specified item type and location within the Repeater control.

C#
protected virtual System.Web.UI.WebControls.RepeaterItem CreateItem(int itemIndex, System.Web.UI.WebControls.ListItemType itemType);

Parameters

itemIndex
Int32

The specified location within the Repeater control to place the created item.

itemType
ListItemType

A ListItemType that represents the specified type of the Repeater item to create.

Returns

The new RepeaterItem object.

Examples

The following code example demonstrates how to override the CreateItem method in a custom server control so that it always returns a new RepeaterItem and corresponding index and type in a custom Repeater server control.

ASP.NET (C#)
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS.Controls" Assembly="Samples.AspNet.CS" %>
<%@ Page Language="C#" AutoEventWireup="True" %>
<!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" >
    <head>
        <title>Custom Repeater - CreateItem - C# Example</title>
    <script language="C#" runat="server">
      void Page_Load(Object Sender, EventArgs e) 
      {
        if (!IsPostBack) 
        {
          ArrayList values = new ArrayList();
          values.Add(new PositionData("Microsoft", "Msft"));
          values.Add(new PositionData("Intel", "Intc"));
          values.Add(new PositionData("Dell", "Dell"));

          Repeater1.DataSource = values;
          Repeater1.DataBind();
        }
      }

      public class PositionData 
      {
        private string name;
        private string ticker;

        public PositionData(string name, string ticker) 
        {
          this.name = name;
          this.ticker = ticker;
        }

        public string Name 
        {
          get 
          {
            return name;
          }
        }

        public string Ticker 
        {
          get 
          {
            return ticker;
          }
        }
      }
    </script>
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <h3>Custom Repeater - CreateItem - C# Example</h3>
            
            <aspSample:CustomRepeaterCreateItem id="Repeater1" runat="server">
              <HeaderTemplate>
                <table border="1" cellpadding="4" cellspacing="0">
                  <tr>
                    <th>Company</th>
                    <th>Symbol</th>
                  </tr>
              </HeaderTemplate>
              <ItemTemplate>
                <tr>
                  <td> <%# DataBinder.Eval(Container.DataItem, "Name") %></td>
                  <td> <%# DataBinder.Eval(Container.DataItem, "Ticker") %></td>
                </tr>
              </ItemTemplate>
              <FooterTemplate>
                </table>
              </FooterTemplate>
          </aspSample:CustomRepeaterCreateItem>
       </form>
    </body>
</html>
C#
using System.Web;
using System.Security.Permissions;

namespace Samples.AspNet.CS.Controls
{
  [AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Minimal)]
  public sealed class CustomRepeaterCreateItem : System.Web.UI.WebControls.Repeater
  {
    protected override System.Web.UI.WebControls.RepeaterItem CreateItem(int itemIndex, System.Web.UI.WebControls.ListItemType itemType)
    {
      // Return a new RepeaterItem with the corresponding item index and type.
      return new System.Web.UI.WebControls.RepeaterItem(itemIndex, itemType);
    }
  }
}

Applies to

Produkt Verzie
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also