Use the pageddatasource class to implement pagination of repeater and datalist

Source: Internet
Author: User

Use the pageddatasource class to implement pagination of repeater and datalist. Pageddatasource encapsulates the pagination attribute of the DataGrid. We can perform pagination like the DataGrid.

HTML code

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>

<! 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 style = "text-align: Center">
<Form ID = "form1" runat = "server">
<Div align = "center" style = "width: 800px;">
<Table cellpadding = "0" cellspacing = "1" border = "0" bgcolor = "#000000" width = "100%">
<Tr>
<TD bgcolor = # ffffff> au_id </TD>
<TD bgcolor = # ffffff> au_fname </TD>
<TD bgcolor = # ffffff> au_lname </TD>
<TD bgcolor = # ffffff> phone </TD>
<TD bgcolor = # ffffff> address </TD>
<TD bgcolor = # ffffff> city </TD>
<TD bgcolor = # ffffff> state </TD>
<TD bgcolor = # ffffff> zip </TD>
<TD bgcolor = # ffffff> contract </TD>
</Tr>
<Asp: repeater runat = "server" id = "rpt_test" onitemdatabound = "rpt_test_itemdatabound">
<Itemtemplate>
<Tr>
<TD bgcolor = # ffffff>
<% # Databinder. eval (container, "dataitem. au_id") %>
</TD>
<TD bgcolor = # ffffff>
<% # Databinder. eval (container, "dataitem. au_fname") %>
</TD>
<TD bgcolor = # ffffff>
<% # Databinder. eval (container, "dataitem. au_lname") %>
</TD>
<TD bgcolor = # ffffff>
<% # Databinder. eval (container, "dataitem. Phone") %>
</TD>
<TD bgcolor = # ffffff>
<% # Databinder. eval (container, "dataitem. Address") %>
</TD>
<TD bgcolor = # ffffff>
<% # Databinder. eval (container, "dataitem. City") %>
</TD>
<TD bgcolor = # ffffff>
<% # Databinder. eval (container, "dataitem. State") %>
</TD>
<TD bgcolor = # ffffff>
<% # Databinder. eval (container, "dataitem.zip") %>
</TD>
<TD bgcolor = # ffffff>
<% # Databinder. eval (container, "dataitem. Contract") %>
</TD>
</Tr>
</Itemtemplate>
</ASP: repeater>
</Table>
</Div>
<Asp: Label id = "lblcurrentpage" runat = "server" text = "label" font-size = 12px> </ASP: Label> & nbsp;
<Asp: hyperlink id = "lnkprev" runat = "server" font-size = 12px> previous page </ASP: hyperlink> & nbsp;
<Asp: hyperlink id = "lnknext" runat = "server" font-size = 12px> next page </ASP: hyperlink>
</Form>
</Body>
</Html>

Code Page

Using system;
Using system. Data;
Using system. configuration;
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 _ default: system. Web. UI. Page
{

# Region page_load ()
Protected void page_load (Object sender, eventargs E)
{
Pageddatasource objds = new pageddatasource ();

Objds. datasource = returndatatable (). defaultview;

Objds. allowpaging = true;

Objds. pagesize = 5;

Int curpage;

If (request. querystring ["page"]! = NULL)
{
Curpage = convert. toint32 (request. querystring ["page"]);
}
Else
{
Curpage = 1;
}

Objds. currentpageindex = curpage-1;

Lblcurrentpage. Text = "Current page:" + curpage. tostring ();

If (! Objds. isfirstpage)
{
Lnkprev. navigateurl = request. currentexecutionfilepath + "? Page = "+ convert. tostring (curpage-1 );
}

If (! Objds. islastpage)
{
Lnknext. navigateurl = request. currentexecutionfilepath + "? Page = "+ convert. tostring (curpage + 1 );
}

Rpt_test.datasource = objds;
Rpt_test.databind ();
}
# Endregion

# Region returns data from the data table
/// <Summary>
/// Return the data in the data table
/// </Summary>
/// <Returns> </returns>
Private datatable returndatatable ()
{

Dataset DS = new dataset ();
Using (sqlconnection Cn = new sqlconnection ("Server = jhtest4; uid = sa; Pwd = 123; database = pubs "))
{
Sqlcommand cmd = new sqlcommand ();
Cmd. Connection = cn;
Cmd. commandtext = "select * from authors ";

Sqldataadapter da = new sqldataadapter (CMD );

Try
{
Da. Fill (DS );
}
Catch (exception ex)
{
Throw ex;
}
}

Return Ds. Tables [0];
}
# Endregion

# Region rpt_test_itemdatabound
/// <Summary>
/// Rpt_test_itemdatabound
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void rpt_test_itemdatabound (Object sender, repeateritemeventargs E)
{

}
# Endregion
}

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.