The ASP.NET Repeater

The ASP.NET Repeater

An ASP.NET Repeater is a very simple control for displaying data. It can be used for displaying data in rows. Because the Repeater doesn't have a custom editor, developers must manually code the HTML. If there is a requirement for a lightweight control with simple requirements of only displaying data in rows, the Repeater will do just fine. Later sections on the DataList and DataGrid controls explain how they differ from the Repeater and when to use them instead.

For working with the Repeater example, create a new ASP.NET application, name the main page WebRepeater.aspx, and add the code in Listing 16.5 as the Web Form, which can be found on the WebRepeater.aspx tab in the Designer. For the database, add BDP components to connect to the Products table in the Northwind database and configure the DataAdapter to select only the ProductName, QuantityPerUnit, and UnitPrice columns. Chapter 15 explains how to set up and configure BDP components.

Listing 16.5 The ASP.NET Repeater Control (WebRepeater.aspx)
 <%@ Page    language="c#"    Debug="true"    Codebehind="WebRepeater.aspx.cs"    AutoEventWireup="false"    Inherits="WebRepeater.WebRepeater" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html>   <head>     <title></title>     <meta name="GENERATOR" content=       "Borland ASP.NET Designer for c# Package Library 7.1">   </head>   <body ms_positioning="FlowLayout">   <form runat="server">     <h1>Products:     </h1>     <p>       <asp:repeater id=repeater1 runat="server">         <headertemplate>         <table border="1" width="100%">           <tr bgcolor="lightblue">             <th>Product Name             </th>             <th>Quantity/Unit             </th>             <th>Unit Price             </th>           </tr>         </headertemplate>         <itemtemplate>         <tr>           <td>             <%# DataBinder.Eval(Container.DataItem,"ProductName") %>           </td>           <td>             <%# DataBinder.Eval(Container.DataItem,"QuantityPerUnit") %>           </td>           <td>             <%# DataBinder.Eval(Container.DataItem,"UnitPrice") %>           </td>         </tr>         </itemtemplate>         <alternatingitemtemplate>         <tr bgcolor="lightgreen">           <td>             <%# DataBinder.Eval(Container.DataItem,"ProductName") %>           </td>           <td>             <%# DataBinder.Eval(Container.DataItem,"QuantityPerUnit") %>           </td>           <td>             <%# DataBinder.Eval(Container.DataItem,"UnitPrice") %>           </td>         </tr>         </alternatingitemtemplate>         <footertemplate>         <tr>           <th colspan="3" align="left" bgcolor="lightblue">             Number of Products:&nbsp;&nbsp;<%# GetNumberOfProducts() %>           </th>         </tr>         </table>         </footertemplate>       </asp:repeater>     </p>   </form>   </body> </html> 

It is possible to drag-and-drop a Repeater control onto the designer surface and set a few properties, but the HTML from Listing 16.5 for the Repeater must still be added. There are four main HTML templates that have been added: headertemplate, itemtemplate, alternatingitemtemplate, and footertemplate. The headertemplate and footertemplate tags create a single header or footer for the Repeater. The only required template is the itemtemplate, which lays out the HTML for each row displayed. The alternatingitemtemplate tag is used to add a new style to every other row displayed. Either the itemtemplate or alternatingitemtemplate will be called for every row in the bound data source.

In ASP.NET, code can be added inline by adding a directive that begins with <%# and ends with %>. In Listing 16.5 this directive is used in two ways: binding columns and calling methods in the code-behind file. The call to DataBinder.Eval(Container.DataItem,"ProductName") obtains the value from the ProductName column of the current row being read. The DataBinder.Eval method will be used for every column to be rendered in the Repeater.

The other usage of the inline directive, used in the footertemplate, is to call the GetNumberOfProducts method in the code-behind file. Listing 16.6 shows the code-behind file, which contains this method.

Listing 16.6 Code-Behind File for Repeater Control Demo (WebRepeater.aspx.cs)
 using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace WebRepeater {    /// <summary>    /// Summary description for WebForm1.    /// </summary>    public class WebRepeater : System.Web.UI.Page    {       protected Borland.Data.Provider.BdpConnection bdpConnection1;       protected Borland.Data.Provider.BdpDataAdapter bdpDataAdapter1;       protected Borland.Data.Provider.BdpCommand bdpSelectCommand1;       protected Borland.Data.Provider.BdpCommand bdpInsertCommand1;       protected Borland.Data.Provider.BdpCommand bdpUpdateCommand1;       protected Borland.Data.Provider.BdpCommand bdpDeleteCommand1;       protected System.Data.DataSet dataSet1;       protected System.Data.DataTable Table1;       protected System.Data.DataColumn dataColumn1;       protected System.Data.DataColumn dataColumn2;       protected System.Data.DataColumn dataColumn3;       protected System.Web.UI.WebControls.Repeater repeater1;       private void Page_Load(object sender, System.EventArgs e)       {          if (!IsPostBack)          {             repeater1.DataSource = Table1;             repeater1.DataBind();          }       }       protected int GetNumberOfProducts()       {          return Table1.Rows.Count;       }       #region Web Form Designer generated code       override protected void OnInit(EventArgs e)       {          InitializeComponent();          base.OnInit(e);       }       /// <summary>       /// Required method for Designer support - do not modify       /// the contents of this method with the code editor.       /// </summary>       private void InitializeComponent()       {          // auto-generated code removed       }       #endregion    } } 

The portions of Listing 16.6 to pay attention to are the Page_Load event handler and the GetNumberOfProducts method. The Page_Load method binds the Table1 DataTable, defined when the DataSet was generated, to the Repeater. Also, notice the call to DataBind. This causes the Repeater to bind to the DataSource. For any of the ASP.NET data controls, leaving this out will prevent the data from showing when the page is rendered. The GetNumberOfProducts method reads the Rows collection of the DataTable that is bound to the Repeater to find out how many Products there are. Figure 16.10 shows what this program looks like when it runs.

Figure 16.10. A Web Form with the ASP.NET Repeater control.

graphics/16fig10.jpg



C# Builder KickStart
C# Builder KickStart
ISBN: 672325896
EAN: N/A
Year: 2003
Pages: 165

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net