asp.net user control for URL pagination

Source: Internet
Author: User
asp.net| Pagination | The control recently made a photo album program frequently needs paging, so I want to write a user control out.

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>
&LT;TD valign= "Middle" height= ">" <asp:label id= "Lb_itemcount" forecolor= "Red" runat= "Server" ></asp: Label> Strip Record </td>
&LT;TD valign= "Middle" height= ><asp:hyperlink id= "Hpl_first" runat= "Server" > Home </asp:hyperlink> </td>
&LT;TD valign= "Middle" height= "><asp:hyperlink id=" Hpl_prev "runat=" "Server" > Prev </asp:hyperlink> </td>
&LT;TD valign= "Middle" height= "> Current <asp:label id=" Lb_currentpage "runat=" Server "></asp:label> page/Total <asp:label id= "Lb_pagecount" runat= "Server" ></asp:label> page </td>
&LT;TD valign= "Middle" height= "><asp:hyperlink id=" Hpl_next "runat=" "Server" > next page </asp:hyperlink> </td>
&LT;TD valign= "Middle" height= ><asp:hyperlink id= "hpl_last" runat= "Server" > Last </asp:hyperlink> </td>
&LT;TD valign= "Middle" height= ><asp:textbox id= "txb_page" runat= "Server" width= "32px" borderstyle= "Solid" Borderwidth= "1px"
Bordercolor= "Silver" ></asp:textbox></td>
&LT;TD valign= "Middle" height= ><asp:imagebutton id= "Btn_go" runat= "Server" imageurl= "Album_images/go.gif" ></asp:ImageButton></td>
&LT;TD valign= "Middle" height= ><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>
A 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 a paging class
PagedDataSource objpage = new PagedDataSource ();
Setting up a data source
Objpage.datasource = dt. DefaultView;
Allow paging
Objpage.allowpaging = true;
Set the number of items to display per 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;
Show 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 home
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;
}


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 the desired method-do not use the Code Editor
Modify the contents 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);
}


}
}

Calls need to set a few parameters pagesize (number of data per page), PAGEP (passed the paging parameter), PARMP (other request.qureystring parameters), URL (page address)

The Datasource=autopage1.databind of the control (DataTable variable) is only required when binding

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.