Repeater page reference 1

Source: Internet
Author: User

The Repeater control is a good thing. Lightweight. Easy to use. Completely custom. However, it is precisely because of these advantages that it does not have the automatic paging function. This requires research. I have read about the novel websites such as the starting point. It is nice to use the Repeater control on the Recommendation rankings. It's too simple. In addition, it is even better to add an UpdatePanel. Like 163 blogs.

OK. After studying the experiment for one morning, I finally wrote my own experiment code. Successfully passed. Of course, success is inseparable from the online sibling. Click here to view references!

This brother may be developed using asp.net 1.x. Therefore, the Code must be modified before it can be run in the editor. Thank you.

PagedDataSource is required for Repeater paging. This class exists in the namespace System. Web. UI. WebControls. It serves as an intermediate medium for data sources and data display controls. For example:

Data Source> PagedDataSource> data binding control

The relationships between them are implemented using the following code:

PagedDataSource PPS = new PagedDataSource ();

PPS. DataSource = dataTable;

Repeater1.DataSource = PPS;

Repeater1.DataBind ();

The code that links the three.

How does PagedDataSource work? Not mentioned above on MSDN. This is just my inference.

PagedDataSource encapsulates the intermediate process of retrieving the page number of data from the underlying data source (such as DataTable. We only need to set

PagedDataSource. AllowPaging = true;

PagedDataSource. PageSize = xx;

PagedDataSource. CurrentPageIndex = currentPage;

The data of the specified page can be retrieved, and the data binding control will get the data from PagedDataSource for display. PagedDataSource is an intermediary here. How does the data binding component retrieve data? How does PagedDataSource retrieve data by page. This is implemented in the asp.net framework and completely transparent to us.

Another important point is that the page number in PagedDataSource starts from 0, not from 1. My sample code is as follows:

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 Buddha. Utils;
Using System. Data. Common;


Public partial class Repeater: System. Web. UI. Page
{


Protected void Page_Load (object sender, EventArgs e)
{
PagedDataSource PPS = new PagedDataSource ();
PPS. DataSource = CreateDataSource (). DefaultView;

// PPS. AllowCustomPaging = true;
PPS. AllowPaging = true;
// PPS. AllowServerPaging = true;
PPS. PageSize = 3;

Int currentPage = Convert. ToInt32 (Request ["page"]);

// Set the current page
Pds. CurrentPageIndex = currentPage;

// Set several hyperlinks
If (! PPS. IsFirstPage)
{
LnkUp. NavigateUrl = Request. CurrentExecutionFilePath + "? Page = "+ (currentPage-1 );
}

If (! PPS. IsLastPage)
{
LnkDown. NavigateUrl = Request. CurrentExecutionFilePath + "? Page = "+ (currentPage + 1 );
}


Lbl_info.Text = "no." + (currentPage + 1) + "Page, total" + pds. PageCount + "page ";

Repeater1.DataSource = PPS;
Repeater1.DataBind ();


}


/// <Summary>
/// Create a data source
/// </Summary>
/// <Returns> </returns>
Private DataTable CreateDataSource ()
{
DataBase db = AppCommon. DefaultDataBase;
DataTable dt = db. GetDataTable ("select id, txt2, txt3, txt4, txt5, txt6 from tbmain1 ");

Db. Dispose ();
Return dt;
}

 

The code in aspx is as follows:

<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Repeater. aspx. cs" Inherits = "Repeater" %>

<! 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> Repeater lab </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: HyperLink ID = "lnkUp" runat = "server"> previous page </asp: HyperLink>
<Asp: HyperLink ID = "lnkDown" runat = "server"> next page </asp: HyperLink>
<Asp: Label ID = "lbl_info" runat = "server" Text = "Current page x, total page x"> </asp: Label> <br/>
<Table>
<Asp: Repeater ID = "Repeater1" runat = "server">
<ItemTemplate>
<Tr> <td> Project name: </td> <% # Eval ("txt2") %> </td> </tr>
<Tr> <td> undertaking organization: </td> <% # Eval ("txt3") %> </td> </tr>
<Tr> <td> scheduler category: </td> <% # Eval ("txt4") %> </td> </tr>
<Tr> <td> technical field: </td> <% # Eval ("txt5") %> </td> </tr>
<Tr> <td colspan = "2"> </ItemTemplate>
</Asp: Repeater>
</Table>
</Div>
</Form>
</Body>
</Html>
Well, the code I wrote is quite refreshing and clear at a glance. Later, I found another piece of code, which was implemented using PagedDataSource. However, it uses LinkButton for navigation, and the result makes the code much more complicated. Because code is required for button events. It is far better to use HyperLinker.

In addition, the best part of my paging code is:

1. You do not need to judge whether Request. QueryString ["page"] is null. There is no need to judge anything for a direct transformation;

Ii. Use IsFirstPage and IsLastPage to determine whether the first and last pages are used

3. The DataTable code is elegant and simple to the extreme. The underlying encapsulation is good.

4. Use HyperLinker instead of LinkButton for navigation.

The PagedDataSource class encapsulates the attributes that allow data source controls (such as DataGrid, GridView, DetailsView, and FormView) to perform paging operations. This type can be used if control developers need to provide paging support for custom data binding controls. It can be seen that if one day you want to bind the data binding control to a data source such as DataTable, you need to use PagedDataSource for manual paging.

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.