Repeater .NET Server Control

Repeater .NET Server Control"-->

only for RuBoard

Repeater .NET Server Control

The Repeater .NET server control is different from the previous controls because it doesn't have an inherent look to it. However, similar to the previous server controls, the Repeater must be bound to a data source to render. The Repeater does exactly what the name states: It repeats. Anything contained within the ItemTemplate is rendered once for each item in the Repeater . DataSource from the top to bottom. Because the Repeater doesn't have a default structure, it's great for creating lists quickly and has less overhead than the DataGrid or DataList .

Tip

The Repeater control is optimized for speed; hence, it has better performance than the DataGrid and DataList . Because you will typically use the Repeater for read only data, use either the SqlDataReader or OleDbDataReader as the data source for performance gains over using the DataSet .


The following list contains all the templates supported by the Repeater control:

The Repeater supports the following five templates:

  • ItemTemplate ” This template contains the HTML elements or server controls that are rendered for each row in the Repeater.DataSource .

  • AlternatingItemTemplate ” Similar to the ItemTemplate but rendered for every other row in the Repeater control. You can have a different look for the data contained in AlternatingItemTemplate .

  • HeaderTemplate and FooterTemplate ” Elements to render once before all data-bound rows have been rendered and once afterward. You'll probably use these templates to open and close an element (for example, a <TABLE> element in the HeaderTemplate and a </TABLE> element in the FooterTemplate ).

  • SeparatorTemplate ” Elements to render between each row, such as line breaks (<BR> tags), lines (<HR> tags), or a comma.

Listing 7.6 contains an example of implementing the Repeater control. In this example we will make use of the HeaderTemplate , ItemTemplate , AlternatingItemTemplate , and FooterTemplate .

Listing 7.6 Using the Repeater Control
 [VisualBasic.NET] 01: <%@ Import Namespace="System.Data" %> 02: <%@ Import Namespace="System.Data.SqlClient" %> 03: 04: <script language="VB" runat="server"> 05: 06:  sub Page_Load(sender as Object, e as EventArgs) 07: 08:   dim SqlCmd as new StringBuilder() 09:   SqlCmd.Append("SELECT S.CompanyName,") 10:   SqlCmd.Append("P.ProductName,") 11:   SqlCmd.Append("P.QuantityPerUnit,") 12:   SqlCmd.Append("P.UnitPrice,") 13:   SqlCmd.Append("P.UnitsInStock,") 14:   SqlCmd.Append("P.UnitsOnOrder ") 15:   SqlCmd.Append("FROM Products P ") 16:   SqlCmd.Append("INNER JOIN Suppliers S ") 17:   SqlCmd.Append("ON ") 18:   SqlCmd.Append("S.SupplierID = P.SupplierID") 19: 20:   dim SqlCon as new SqlConnection("server=localhost; graphics/ccc.gif uid=sa;pwd=;database=northwind") 21:   dim sqlcommand as new SqlCommand(SqlCmd.ToString(), SqlCon) 22: 23:   SqlCon.Open() 24:    RProducts.DataSource = sqlcommand.ExecuteReader() 25:    RProducts.DataBind() 26:   SqlCon.Close() 27: 38: End Sub 29: 30: </script> [C#.NET] 01: <%@ Import Namespace="System.Data" %> 02: <%@ Import Namespace="System.Data.SqlClient" %> 03: 04: <script language="c#" runat="server"> 05: 06:  public void Page_Load(Object sender, EventArgs e) { 07: 08:   StringBuilder SqlCmd = new StringBuilder(); 09:   SqlCmd.Append("SELECT S.CompanyName,"); 10:   SqlCmd.Append("P.ProductName,"); 11:   SqlCmd.Append("P.QuantityPerUnit,"); 12:   SqlCmd.Append("P.UnitPrice,"); 13:   SqlCmd.Append("P.UnitsInStock,"); 14:   SqlCmd.Append("P.UnitsOnOrder "); 15:   SqlCmd.Append("FROM Products P "); 16:   SqlCmd.Append("INNER JOIN Suppliers S "); 17:   SqlCmd.Append("ON "); 18:   SqlCmd.Append("S.SupplierID = P.SupplierID"); 19: 20:   SqlConnection SqlCon = new SqlConnection("server=localhost; graphics/ccc.gif uid=sa;pwd=;database=northwind"); 21:   SqlCommand sqlcommand = new SqlCommand(SqlCmd.ToString(), SqlCon); 22: 23:   SqlCon.Open(); 24:    RProducts.DataSource = sqlcommand.ExecuteReader(); 25:    RProducts.DataBind(); 26:   SqlCon.Close(); 27: 28: } 29: 30: </script> [VisualBasic.NET and C#.NET] 31: <html> 32: <head><title>DotNetJunkies.com</title> 33: <style rel="stylesheet"> 34: 35:  A { text-decoration:none;color:black} 36:  A:Hover { color:maroon;text-decoration:underline} 37:  A:Visited { color:red} 38:  .body {  font: 9pt Verdana, Arial, sans-serif; } 39:  .head {  font: bold 9pt Verdana, Arial,   sans-serif; background-color:Maroon; color: graphics/ccc.gif white; } 40:  .tablehead {  font: bold 9pt Verdana, Arial,   sans-serif; background-color:green; graphics/ccc.gif color:white; } 41: 42: </style> 43: 44: </head> 45: <body> 46: <center> 47: 48:  <asp:Repeater 49:   id="RProducts" 50:   Runat="Server" 51:  > 52: 53:       <HeaderTemplate> 54: 55:        <table CellPadding="4" CellSpacing="0"   rules="all" border="1" style="width: graphics/ccc.gif 500;border-collapse:collapse;"> 56:         <tr> 57:          <td class="Head"> 58:           <center> 59:            Northwind Products 60:           </center> 61:          </td> 62:         </tr> 63: 64:       </HeaderTemplate> 65: 66:       <ItemTemplate> 67: 68:        <tr> 69:         <td> 70:         <table width="100%" Class="body"> 71:          <tr> 72:           <td align="center" class="tablehead" ColSpan="2"> 73:            <%# DataBinder.Eval(Container.DataItem, "ProductName" )%> 74:           </td> 75:          </tr> 76:          <tr> 77:           <td class="body" align="left" valign="top"> 78:             <b>Price</b> 79:           </td> 80:           <td class="body" align="right" valign="top"> 81:            <b><%# DataBinder.Eval(   Container.DataItem, "UnitPrice",  "{ 0:C} ")%></ graphics/ccc.gif b> 82:           </td> 83:          </tr> 84:          <tr> 85:           <td class="body" align="left" valign="top"> 86:            <b>Quantity Per Unit</b> 87:           </td> 88:           <td class="body" align="right" valign="top"> 89:            <b><%# DataBinder.Eval(   Container.DataItem, "QuantityPerUnit")%></b> 90:           </td> 91:          </tr> 92:          <tr> 93:           <td align="center" class="head" ColSpan="2"></td> 94:          </tr> 95:          <tr> 96:           <td class="body" ColSpan="2" align="center"> 97:            <a Target="_blank" 98:            href="ProductDetails.aspx?ProductName=<%# DataBinder.Eval( graphics/ccc.gif Container.DataItem, "ProductName")%>"> 99:             [ More Information Click Here ] 100:            </a> 101:           </td> 102:          </tr> 103:         </table> 104:        </td> 105:       </tr> 106: 107:      </ItemTemplate> 108: 109:      <AlternatingItemTemplate> 110: 111:       <tr> 112:        <td> 113:         <table width="100%"> 114:          <tr> 115:           <td align="center" class="Head" ColSpan="2"> 116:            <%# DataBinder.Eval(Container.DataItem, "ProductName" )%> 117:           </td> 118:          </tr> 119:          <tr> 120:           <td class="body" ColSpan="2" align="center"> 121:            <a Target="_blank" 122:            href="ProductDetails.aspx?ProductName=<%# DataBinder.Eval( graphics/ccc.gif Container.DataItem, "ProductName")%>"> 123:            [ More Information Click Here ] 124:            </a> 125:           </td> 126:          </tr> 127:          <tr> 128:           <td align="center" class="head" ColSpan="2"></td> 129:          </tr> 130:          <tr> 131:           <td class="body" align="left" valign="top"> 132:            <b>Price</b> 133:           </td> 134:           <td class="body" align="right" valign="top"> 135:            <b><%# DataBinder.Eval(   Container.DataItem, "UnitPrice",  "{ 0:C} ")%></ graphics/ccc.gif b> 136:           </td> 137:          </tr> 138:          <tr> 140:           <td class="body" align="left" valign="top"> 141:            <b>Quantity Per Unit</b> 142:           </td> 143:           <td class="body" align="right" valign="top"> 144:            <b><%# DataBinder.Eval(   Container.DataItem, "QuantityPerUnit")%></b> 145:           </td> 146:          </tr> 147:         </table> 148:        </td> 149:       </tr> 150: 151:      </AlternatingItemTemplate> 152: 153:     <FooterTemplate> 154: 155:      </table> 156: 157:     </FooterTemplate> 158: 159:  </asp:Repeater> 160: </center> 161: </body> 162: </html> 

The Repeater is different from the previous two template controls in a few different ways. First, notice that the Repeater has no default structure ”it simply repeats. This is why there are no attributes such as ItemTemplate-Style as with the DataGrid or DataList controls. Instead, when using the Repeater you must define all of its output structure and style.

Let's walk through each of the different sections of the Repeater . First, the HeaderTemplate (lines 53 “64) ”this section of the Repeater will only be rendered once. This section is where you would most likely put the Header for the output. In this example I first start a <table> HTML element to provide structure to the output data and for the header I text I used, "Northwind Products."

The next section is the ItemTemplate (lines 66 “107) ”the ItemTemplate , like the DataList and DataGrid is rendered once for each item in the Repeater.DataSource unless the AlternatingItemTemplate is being used. Within the ItemTemplate I nest an additional table to achieve even more control over the way each item is rendered. The ItemTemplate behaves exactly like the DataList and DataGrid 's ItemTemplate ”note: The more complicated the structure is in the ItemTemplate , the slower the rendering.

The AlternatingItemTemplate is the next item in the Repeater . It too behaves like the DataList 's AlternatingItemTemplate . In this example we create another nested table. The next template is something you haven't used yet ”the FooterTemplate . In this example I use the FooterTemplate to close our <TABLE> HTML tag. Figure 7.6 contains an illustration of this page when rendered.

Figure 7.6. Repeater server control using the HeaderTemplate , ItemTemplate , AlternatingItemTemplate , and FooterTemplate .
graphics/07fig06.gif
only for RuBoard


Programming Data-Driven Web Applications with ASP. NET
Programming Data-Driven Web Applications with ASP.NET
ISBN: 0672321068
EAN: 2147483647
Year: 2000
Pages: 170

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