Source: snooker_li's column.
I recently made a photo album program that frequently required paging, so I wanted to write a user control.
The code is as follows:
AutoPage.ascx page
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="AutoPage.ascx.cs" Inherits="album.AutoPage" TargetSchema=" http://schemas.microsoft.com/ intellisense/ie5 " %>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="middle" height="30">Total <asp:label id="lb_ItemCount" ForeColor="Red" runat="server"></asp:label> records </td>
<td valign="middle" height="30"><asp:hyperlink id="hpl_First" runat="server">Homepage</asp:hyperlink> </td>
<td valign="middle" height="30"><asp:hyperlink id="hpl_Prev" runat="server">Previous page</asp:hyperlink> </td>
<td valign="middle" height="30">Current<asp:label id="lb_CurrentPage" runat="server"></asp:label>pages/total<asp:label id="lb_PageCount" runat=" server"></asp:label>page </td>
<td valign="middle" height="30"><asp:hyperlink id="hpl_Next" runat="server">Next page</asp:hyperlink> </td>
<td valign="middle" height="30"><asp:hyperlink id="hpl_Last" runat="server">Last page</asp:hyperlink> </td>
<td valign="middle" height="30"><asp:textbox id="txb_Page" runat="server" Width="32px" BorderStyle="Solid" BorderWidth="1px"
BorderColor="Silver"></asp:textbox></td>
<td valign="middle" height="30"><asp:ImageButton id="btn_go" runat="server" ImageUrl="album_images/go.gif"></asp:ImageButton></td>
<td valign="middle" height="30"><asp:label id="lb_url" runat="server" Visible="False"></asp:label><asp:Label id="lb_Params" runat= "server" Visible="False"></asp:Label></td>
</tr>
</table>
AutoPage.ascx.cs page
namespace album
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// Summary description of UC.
/// </summary>
public class AutoPage : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.HyperLink hpl_First;
protected System.Web.UI.WebControls.HyperLink hpl_Prev;
protected System.Web.UI.WebControls.HyperLink hpl_Next;
protected System.Web.UI.WebControls.Label lb_CurrentPage;
protected System.Web.UI.WebControls.Label lb_PageCount;
protected System.Web.UI.WebControls.HyperLink hpl_Last;
public int pagesize;
public string PageP;
protected System.Web.UI.WebControls.TextBox txb_Page;
protected System.Web.UI.WebControls.Label lb_url;
protected System.Web.UI.WebControls.Label lb_ItemCount;
public string url;
protected System.Web.UI.WebControls.Label lb_Params;
protected System.Web.UI.WebControls.ImageButton btn_go;
public string Params;
private void Page_Load(object sender, System.EventArgs e)
{
}
public PagedDataSource databind(DataTable dt)
{
lb_url.Text = url;
lb_Params.Text = Params;
//Create paging class
PagedDataSource objPage = new PagedDataSource();
//Set data source
objPage.DataSource = dt.DefaultView;
//Allow paging
objPage.AllowPaging = true;
//Set the number of items displayed on each page
objPage.PageSize = pagesize;
//Set the index of the current page
int CurPage=1;
try
{
CurPage = Convert.ToInt32(PageP);
if (CurPage<1 || CurPage>objPage.PageCount)
{
Response.Redirect(url+"?page=1"+Params);
}
}
catch
{
Response.Redirect(url+"?page=1"+Params);
}
objPage.CurrentPageIndex = CurPage-1;
//Display status information
lb_ItemCount.Text = dt.Rows.Count.ToString();
lb_CurrentPage.Text = CurPage.ToString();
lb_PageCount.Text =objPage.PageCount.ToString();
//If the current page is not the home page
if (!objPage.IsFirstPage)
{
hpl_Prev.NavigateUrl=url + "?Page=" + Convert.ToString(CurPage-1)+Params;
hpl_First.NavigateUrl=url + "?Page=1"+Params;
}
//If the current page is not the last page
if (!objPage.IsLastPage)
{
hpl_Next.NavigateUrl=url+ "?Page=" + Convert.ToString(CurPage+1)+Params;
hpl_Last.NavigateUrl=url + "?Page=" +objPage.PageCount.ToString()+Params;
}
return objPage;
}
#region Web Forms Designer Generated Code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Forms designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Designer supports required methods - do not use code editor
/// Modify the content of this method.
/// </summary>
private void InitializeComponent()
{
this.btn_go.Click += new System.Web.UI.ImageClickEventHandler(this.btn_go_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btn_go_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
Response.Redirect(lb_url.Text+"?Page="+txb_Page.Text+lb_Params.Text);
}
}
}
When calling, you need to set several parameters pagesize (the number of data displayed on each page), PageP (passed paging parameters), ParmP (other Request.QureyString parameters), and url (page address).
When binding, you only need to bind the control DataSource=AutoPage1.databind(DataTable variable)