datalist分页(codebehind部分)
2024-07-21 02:16:47
供稿:网友
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;
//设定pagesize
pagesize = 10;
//计算总共有多少记录
recordcount = calculaterecord();
lblrecordcount.text = recordcount.tostring();
if(!page.ispostback)
{
dosearch();
currentpage = 0;
viewstate["pageindex"] = 0;
//计算总共有多少页
pagecount = recordcount/pagesize;
if (recordcount%pagesize > 0)
pagecount = pagecount + 1;
lblpagecount.text = pagecount.tostring();
viewstate["pagecount"] = pagecount;
}
}
}
//计算总共有多少条记录
public int calculaterecord()
{
cj168.dataaccess.magsdb dosearch = new cj168.dataaccess.magsdb();
return dosearch.getsearchresultcount(txtsearch);
}
void dosearch()
{
int startindex;
int fromitem, toitem;
//设定导入的起终地址
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;
//判断cmd,以判定翻页方向
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 call is required by the asp.net web form designer.
//
initializecomponent();
}
#region web form designer generated code
/// <summary>
/// required method for designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void initializecomponent()
{
this.load += new system.eventhandler(this.page_load);
}
#endregion
}
}