About the use of repeater controls

Source: Internet
Author: User
Tags eval expression visual studio
Control about the use of repeater controls



The Repeater control is a data display control that allows you to customize the layout by repeatedly using the specified template for each item displayed in the list.



To display data, you must first create a template to bind the list of data, which is defined below (see also SDK):



Template
Description

AlternatingItemTemplate
Similar to the ItemTemplate element, but is rendered once (alternating items) in the Repeater control. You can specify a different appearance for a alternatingitemtemplate element by setting its style properties.

FooterTemplate
The element that is rendered once after all data-bound rows are rendered. A typical use is to turn off elements that are open in the HeaderTemplate entry (using a tag such as </table>).

Note that FooterTemplate cannot be data-bound.

HeaderTemplate
The element that is rendered once before all data-bound rows are rendered. A typical use is to start a container element (such as a table).

Note that the HeaderTemplate item cannot be data-bound.

ItemTemplate
An element that renders one time for each row in the data source. To display data from ItemTemplate, declare one or more Web server controls and set their data-binding expressions so that they are evaluated as fields in the DataSource of the Repeater control (that is, the container control). The following example displays an example declaration that displays the field that contains the first name in the Label control.

The Name:
<asp:label runat= "Server"
text= "<%# Container.DataItem.FirstName%>"/>

SeparatorTemplate
The elements that are rendered between rows are usually line breaks (<br> marks), horizontal lines (
Note that the SeparatorTemplate item cannot be data-bound.


Note: The control cannot be visualized by editing the template, and the DataList DataGrid control is OK.



Let's talk about the process of creating a program:

1. Create a Web application and rename the default Web Form to: repeater.aspx.

2, switch to the HTML view, enter the following code:

<%@ Page language= "C #" codebehind= "Repeater.aspx.cs" autoeventwireup= "false" inherits= " TeachShow.Charpter7.Repeater "%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >

<HTML>

<HEAD>

<title>Repeater</title>

<link rel= "stylesheet" type= text/css "href=". /style.css ">

<meta name= "generator" content= "Microsoft Visual Studio. NET 7.1" >

<meta name= "Code_language" content= "C #" >

<meta name= "vs_defaultClientScript" content= "JavaScript" >

<meta name= "vs_targetschema" content= "http://schemas.microsoft.com/intellisense/ie5" >

</HEAD>

<body ms_positioning= "GridLayout" >

<form id= "Form1" method= "POST" runat= "Server" >

<div align= "center" >

<center>

<table border= "0" cellpadding= "0" cellspacing= "0" width= "272" height= "136" >

<tr>

&LT;TD width= "272" height= "136" >

<div align= "center" >

<center>

<table border= "1" cellpadding= "0" cellspacing= "0" width= "272" height= "a" bordercolorlight= "#000000"

bordercolordark= "#ffffff" class= "smallred" >

<asp:repeater id= "Repeater1" runat= "Server" >

<HeaderTemplate>

<tr>

&LT;TD width= "height=" ><font face= "song body" > Digital </FONT></td>

&LT;TD width= "height=" ><font face= "song body" > Square </FONT></td>

&LT;TD width= "height=" ><font face= "song body" > Cubic </FONT></td>

</tr>

</HeaderTemplate>

<ItemTemplate>

<tr>

&LT;TD width= "height=" ><%# DataBinder.Eval (Container.DataItem, "number")%></td>

&LT;TD width= "height=" ><%# DataBinder.Eval (Container.DataItem, "square")%></td>

&LT;TD width= "height=" ><%# DataBinder.Eval (Container.DataItem, "cubic")%></td>

</tr>

</ItemTemplate>

</asp:Repeater>

</table>

</center>

</div>

</td>

</tr>

</table>

</center>

</div>

</form>

</body>

</HTML>



Explain the methods used in the program:

DataBinder.Eval () Method: This method is used to compute a data-binding expression at run time and to format the output according to the needs of the browser. The method has three parameters:

A, the name container for the data item: The naming container is an object reference that is the object to which the expression is evaluated. If the binding is for a list control (such as a Repeater, DataList, or DataGrid), the naming container will always be container.dataitem. If the binding is for a page, the naming container is page.

B, data field name: The column name of the binding table (such as "squared", and so on).

C, format string

DataBinder.Eval () method is not recommended if high performance is required



3, open the Repeater.aspx.cs file, enter the following code:

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 Teachshow.charpter7

{

<summary>

Summary description of the Repeater.

</summary>

public class Repeater:System.Web.UI.Page

{

protected System.Web.UI.WebControls.Repeater Repeater1;



private void Page_Load (object sender, System.EventArgs e)

{

Place user code here to initialize page

if (!this. IsPostBack)

{

DataTable mydt=new DataTable ();

DataRow myDR;

myDT. Columns.Add (New DataColumn ("number", typeof (Int32));

myDT. Columns.Add (New DataColumn ("Square", typeof (Int32));

myDT. Columns.Add (New DataColumn ("Cubic", typeof (Int32));



for (int i=0;i<=10;i++)

{

Mydr=mydt. NewRow ();

Mydr[0]=i;

Mydr[1]=i*i;

Mydr[2]=i*i*i;

myDT. Rows.Add (MYDR);

}



This. Repeater1.datasource=mydt;

This. Repeater1.databind ();

}

}



Code generated #region the Web forms Designer

Override protected void OnInit (EventArgs e)

{

//

CodeGen: This call is required for the ASP.net Web forms Designer.

//

InitializeComponent ();

Base. OnInit (e);

}



<summary>

Designer supports required methods-do not use the Code editor to modify

The contents of this method.

</summary>

private void InitializeComponent ()

{

This. Repeater1.itemcommand + = new System.Web.UI.WebControls.RepeaterCommandEventHandler (this. Repeater1_itemcommand);

This. Load + = new System.EventHandler (this. Page_Load);



}

#endregion



private void Repeater1_itemcommand (object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)

{



}

}

}

4, the last browsing, to see what results? See table below:

Digital
Square
Cubic

0
0
0

1
1
1

2
4
8

3
9
27

4
16
64

5
25
125

6
36
216

7
49
343

8
64
512

9
81
729

10
100
1000




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.