datalist分页(codebehind部分)
发表于:2007-06-30来源:作者:点击数:
标签:
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; usi
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.DataA
clearcase/" target="_blank" >ccess.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
}
}
原文转自:http://www.ltesting.net