The Repeater control is a container control in the Web server control. It allows you to create a custom list from any available data on the page. The Repeater control does not have a built-in rendering function, which means that you must provide a layout for the Repeater control by creating a template. When this page is running, the Repeater control presents one item for each record in turn through the records in the data source. He is very simple. He is not powerful enough to use it. But as the old saying goes, what is the use of a knife? Can't we bite the chicken? In our daily applications, we often face this kind of choice. If we show that we can teach complex data, we can use the gridview, which is generally complicated to use DataList. What about simple data presentation? We use Repeater because it is simple, small, and most importantly flexible. as we mentioned above, he does not have the built-in rendering function, so we need to use some templates to present his data. by the way, when it comes to presentation, I have a lot to talk about. He hasn't added the static item Function to you. In other words, he only deals with the database, and the average person doesn't talk to you. the following table describes the templates supported by the Repeater control. The template attribute description ItemTemplate contains the HTML elements and controls that must be presented once for each data item in the data source. AlternatingItemTemplate contains the HTML elements and controls that must be presented once for each data item in the data source. Generally, you can use this template to create different appearances for alternate items. For example, you can specify a background color that is different from the color specified in ItemTemplate. HeaderTemplate and FooterTemplate are the texts and controls displayed at the beginning and end of the list. SeparatorTemplate contains the elements rendered between each item. A typical example may be a straight line (using the hr element ). We define the format in these templates. When the webpage is displayed, it will be pretty. Of course, if you are ugly, it cannot be beautiful. in addition, the Item sample must be defined. If the Repeater control does not specify a data source, it will not be displayed. If no data exists in the specified data source, the script template will continue to be displayed. For this control, we will mainly talk about the following points: 1. Understand several templates of the Control 2. The Repeater control is simple and practical to read and present data. 3. Set this control. 4. implement data paging. (I previously published the control pagination on the repeater page, but it is not easy to see, this time, we put the pages in the footer template to make the controls more beautiful.) 5. Let's turn our data entries into links. Only in this way can we complete them.
======================================
Front-end
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "repeart2.aspx. cs" Inherits = "repeart_repeart2" %>
<% @ 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">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
Repeater enables paging and data binding by itself (without data sources) <br/>
Use Repeater for Mosaic. <br/>
<Br/>
<Asp: Repeater ID = "Repeater1" runat = "server" OnItemDataBound = "repeaterincluitemdatabound">
<HeaderTemplate>
<Table>
<Tr>
<Td>
Author
</Td>
<Td>
Books
</Td>
</Tr>
</HeaderTemplate>
<ItemTemplate>
<Tr>
<Td>
<A href = "repeart1.aspx"> <% # Eval ("au_lname") %> </a>
</Td>
<Td>
<% -- The second Repeater is placed in the ItemTemplate of the first Repeater. -- %>
<Asp: Repeater ID = "Repeater2" runat = "server" datasource = '<% # Eval ("myrela") %>'>
<ItemTemplate>
<% -- Note the Writing Method -- %>
<% # Eval ("[\" title_id \ "]") %> <br>
</ItemTemplate>
</Asp: Repeater>
</Td>
</Tr>
</ItemTemplate>
<SeparatorTemplate>
<Tr>
<Td colspan = "2">
<Hr size = "1pt"/>
</Td>
</Tr>
</SeparatorTemplate>
<FooterTemplate>
<Tr>
<Td colspan = "2">
Total <asp: Label ID = "lblpTotal" runat = "server" Text = "Label"> </asp: Label> pages are currently <asp: label ID = "lblpCurrent" runat = "server" Text = "Label"> </asp: Label> page
<Asp: HyperLink ID = "hlfirst" runat = "server" Text = "Homepage"> </asp: HyperLink>
<Asp: HyperLink ID = "hlup" runat = "server" Text = "Previous Page"> </asp: HyperLink>
<Asp: HyperLink ID = "hlnext" runat = "server" Text = "Next"> </asp: HyperLink>
<Asp: HyperLink ID = "hllast" runat = "server" Text = "last page"> </asp: HyperLink>
</Td>
</Tr>
</Table>
</FooterTemplate>
</Asp: Repeater>
</Div>
</Form>
</Body>
</Html>
========================================================== =====
Background;
Using System;
Using System. Data;
Using System. Configuration;
Using System. Collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Data. SqlClient;
Public partial class repeart_repeart2: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! Page. IsPostBack)
{
This. Repeater1.DataSource = pds ();
This. Repeater1.DataBind ();
}
}
Public PagedDataSource ()
{
// String connstring = ConfigurationManager. ConnectionStrings ["Pubs"]. ConnectionString;
//// Declare a string that can be used at any time.
// SqlConnection con = new SqlConnection (connstring );
//// Initialize the connection
// SqlDataAdapter sda = new SqlDataAdapter ("select * from authors", con );
//// Initialize a SqlDataAdapter and provide a query statement
// DataSet ds = new DataSet ();
//// Initialize a DataSet
// Sda. Fill (ds, "name ");
//// Fill the data queried above in the name table
// SqlDataAdapter sda2 = new SqlDataAdapter ("select * from titleauthor", con );
/// Same as above
// Sda2.Fill (ds, "title ");
/// Same as above
// Ds. relations. add ("myrela", ds. tables ["name"]. columns ["au_id"], ds. tables ["title"]. columns ["au_id"]);
//// Create a link for the two tables created above, specify the names of the Parent and Child columns, and name their relationships.
String connstring = ConfigurationManager. ConnectionStrings ["pubs"]. ConnectionString;
SqlConnection con = new SqlConnection (connstring );
SqlDataAdapter sda = new SqlDataAdapter ("select * from authors", con );
DataSet ds = new DataSet ();
Sda. Fill (ds, "name ");
SqlDataAdapter sda2 = new SqlDataAdapter ("select * from titleauthor", con );
Sda2.Fill (ds, "title ");
Ds. relations. add ("myrela", ds. tables ["name"]. columns ["au_id"], ds. tables ["title"]. columns ["au_id"]);
// PagedDataSource PPS = new PagedDataSource ();
//// Initialize a PagedDataSource that allows controls to be paged
// PSP. DataSource = ds. Tables ["name"]. DefaultView;
//// Convert the above ds to a standard data view
// PPS. AllowPaging = true;
//// Allow Paging
// PSP. PageSize = 5;
/// The page size is 5
// PSP. CurrentPageIndex = Convert. ToInt32 (Request. QueryString ["page"]);
//// Set the current page
// Return PSP;
//// Send the processed pds object
PagedDataSource PPS = new PagedDataSource ();
Pds. DataSource = ds. Tables ["name"]. DefaultView;
PPS. AllowPaging = true;
PPS. PageSize = 5;
PPS. CurrentPageIndex = Convert. ToInt32 (Request. QueryString ["page"]);
Return PPS;
}
Protected void repeaterincluitemdatabound (object sender, RepeaterItemEventArgs e)
{
If (e. Item. ItemType = ListItemType. Footer)
{
// Int n = pds (). PageCount; // assign the total number of pages to Variable n
// Int I = pds (). CurrentPageIndex; // assign the current page number
// Label lblpc = (Label) e. Item. FindControl ("lblpc ");
// Lblpc. Text = n. ToString ();
/// Locate the lblpc Label and grant the total page number to him.
// Label lblp = (Label) e. Item. FindControl ("lblp ");
// Lblp. Text = Convert. ToString (pds (). CurrentPageIndex + 1 );
//// Locate the lblp Label and assign the current page number to it. However, note that because the page number starts from 0, you must add 1 if it is intuitive.
// HyperLink hlfir = (HyperLink) e. Item. FindControl ("hlfir ");
// Hlfir. NavigateUrl = "? Page = 0 ";
// HyperLink hlla = (HyperLink) e. Item. FindControl ("hlla ");
// Hlla. NavigateUrl = "? Page = "+ Convert. ToInt32 (n-1 );
/// Locate the Label that indicates the front and last pages, and assign the NavigateUrl attribute to 0th pages and the maximum page number minus 1
// HyperLink hlp = (HyperLink) e. Item. FindControl ("hlp ");
// HyperLink hln = (HyperLink) e. Item. FindControl ("hln ");
// Ssssssssssssssssssssss
Int n = PPS (). PageCount; // the total number of pages.
Int I = PPS (). CurrentPageIndex; // current page number
// The total number of pages in the next two rows
Label lblpTotal = (Label) e. Item. FindControl ("lblpTotal ");
LblpTotal. Text = n. ToString ();
// The next two rows are the current page number
Label lblpCurrent = (Label) e. Item. FindControl ("lblpCurrent ");
LblpCurrent. Text = Convert. ToString (pds (). CurrentPageIndex + 1); // the page number starts from 0.
// Homepage, last page
HyperLink hlfirst = (HyperLink) e. Item. FindControl ("hlfirst ");
Hlfirst. NavigateUrl = "? Page = 0 ";
HyperLink hllast = (HyperLink) e. Item. FindControl ("hllast ");
Hllast. NavigateUrl = "? Page = "+ Convert. ToInt32 (n-1 );
// Note: declare it first.
HyperLink hlup = (HyperLink) e. Item. FindControl ("hlup ");
HyperLink hlnext = (HyperLink) e. Item. FindControl ("hlnext ");
// Locate the two controls that indicate the previous and next pages
If (I <= 0)
{// If the current page is already 0th
Hlup. Enabled = false;
Hlfirst. Enabled = false;
Hlnext. Enabled = true;
}
Else
{
Hlup. NavigateUrl = "? Page = "+ Convert. ToInt32 (I-1 );
}
If (I> n-2)
{// If the current item is already the last page
Hlnext. Enabled = false;
Hllast. Enabled = false;
Hlup. Enabled = true;
}
Else
{
Hlnext. NavigateUrl = "? Page = "+ Convert. ToInt32 (I + 1 );
}
}
}
}