DataList pagination (Codebehind part)

Source: Internet
Author: User
Paging using System;
Using System.Collections;
Using System.Configuration;
Using System.ComponentModel;
Using System.Data;
Using System.Data.SqlClient;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;

Namespace cj168. Web
{
<summary>
Summary description for search.
</summary>
public class Search:System.Web.UI.Page

{
protected System.Web.UI.WebControls.DataList Datalistsearchresult;
protected System.Web.UI.WebControls.Label Lbltxtsearch;
protected System.Web.UI.WebControls.Label Lblrecordcount;
protected System.Web.UI.WebControls.Label lblcurrentpage;
protected System.Web.UI.WebControls.Label Lblpagecount;
protected System.Web.UI.WebControls.Label Lblpagerange;

protected System.Web.UI.WebControls.LinkButton lbnprevpage;
protected System.Web.UI.WebControls.LinkButton lbnnextpage;
String Txtsearch;
int pagesize,recordcount,pagecount,currentpage;


Public Search ()
{
Page.Init + = new System.EventHandler (Page_Init);
}

private void Page_Load (object sender, System.EventArgs e)
{
Put user code to initialize the page here
Txtsearch = request.params["Txtsearch"];
if (Txtsearch!= null)
{Txtsearch = Txtsearch.replace ("'", "");

Lbltxtsearch.text = Txtsearch;

Set pagesize
PageSize = 10;

Calculate how many records are in total
RecordCount = Calculaterecord ();
Lblrecordcount.text = Recordcount.tostring ();

if (! Page.IsPostBack)

{


Dosearch ();
currentpage = 0;
viewstate["PageIndex"] = 0;



Calculate how many pages are in total
PageCount = recordcount/pagesize;
if (recordcount%pagesize > 0)
PageCount = PageCount + 1;
Lblpagecount.text = Pagecount.tostring ();
viewstate["PageCount"] = PageCount;


}
}




}

Calculate how many records are in total
public int Calculaterecord ()
{
cj168. Dataaccess.magsdb dosearch = new cj168. Dataaccess.magsdb ();
Return Dosearch.getsearchresultcount (Txtsearch);

}



void Dosearch ()
{
int StartIndex;
int Fromitem, Toitem;

Set the start address of the import
StartIndex = currentpage*pagesize;

cj168. Dataaccess.magsdb dosearch = new cj168. Dataaccess.magsdb ();
Datalistsearchresult.datasource = Dosearch.getsearchresult (Txtsearch,startindex, PageSize);
Datalistsearchresult.databind ();

Lbnnextpage.enabled = true;
Lbnprevpage.enabled = true;
if (currentpage== (PageCount-1)) lbnnextpage.enabled = false;
if (currentpage==0) lbnprevpage.enabled = false;
Lblcurrentpage.text = (currentpage+1). ToString ();

Fromitem = startindex+1;
Toitem = startindex+pagesize;

if (RecordCount < Toitem)
Toitem = RecordCount;

Lblpagerange.text = Fromitem + "-" + toitem;

}




public void Page_onclick (Object Sender,commandeventargs e)
{
currentpage = (int) viewstate["PageIndex"];
PageCount = (int) viewstate["PageCount"];

string cmd = E.commandname;
Judge Cmd to determine the direction of page flipping
Switch (CMD)
{
Case "Next":
if (currentpage< (PageCount-1)) currentpage++;
Break
Case "Prev":
if (currentpage>0) currentpage--;
Break
}

viewstate["PageIndex"] = currentpage;

Dosearch ();


}



private void Page_Init (object sender, EventArgs e)
{
//
Codegen:this the call are required by the ASP.net Web Form Designer.
//
InitializeComponent ();
}

#region Web Form Designer generated code
<summary>
Required to Designer support-do not modify
The contents is with the Code Editor.
</summary>
private void InitializeComponent ()
{
This. Load + = new System.EventHandler (this. Page_Load);

}
#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.