翻页控件
/// <summary>
/// Summary description for Pager.
/// </summary>
public class Pager : BaseControl, System.Web.UI.IPostBackEventHandler
{
public Pager()
{
this.Load += new EventHandler(Pager_Load);
}
private void Pager_Load(object sender,EventArgs e)
{
}
protected override void Render(HtmlTextWriter output)
{
if(this.PageCount == 0)
{
return;
}
if(LinkedPager!=null)
{
Pager linkedPager = (Pager)Parent.FindControl(LinkedPager);
if(linkedPager==null)
throw new Exception(string.Format("Failed to link pager to '{0}'.",LinkedPager));
linkedPager.Render(output);
return;
}
//显示页次
output.WriteLine("<span>");
output.WriteLine("页次:{0}/{1} ",CurrentPageIndex+1,PageCount);
//跳转
output.WriteLine("<script LANGUAGE='javascript'>");
output.WriteLine("function formHandler()");
output.WriteLine("{ ");
output.WriteLine("var xx = document.getElementById('Select1');");
output.WriteLine("var URL = xx.options[xx.selectedIndex].value; ");
output.WriteLine("window.location.href = URL; ");
output.WriteLine("} ");
output.WriteLine("</script> ");
output.WriteLine("转到第<select NAME='site' SIZE='1' onChange='formHandler()' ID='Select1'>");
for(int i= 0;i<PageCount; i++)
{
if(i== CurrentPageIndex)
output.WriteLine("<option value={0} selected >{1}</option>", Page.GetPostBackClientHyperlink(this,i.ToString()),i+1);
else
output.WriteLine("<option value={0}>{1}</option>", Page.GetPostBackClientHyperlink(this,i.ToString()),i+1);
}
output.WriteLine("</select>页 ");
output.WriteLine("</span>");
//显示上下翻页
int iLastPageN0 = CurrentPageIndex-1;
int iNextPageN0 = CurrentPageIndex+1;
if(CurrentPageIndex<=0)
{
//output.WriteLine("<a href=\"{0}\" style='color:black;'>上页</a>",Page.GetPostBackClientHyperlink(this,CurrentPageIndex.ToString()));
}
else
{
output.WriteLine("<a href=\"{0}\" style='color:#0000FF;'>上页</a>",Page.GetPostBackClientHyperlink(this,iLastPageN0.ToString()));
}
if(CurrentPageIndex>0&CurrentPageIndex<(PageCount-1))
{
output.WriteLine(" | ");
}
if(CurrentPageIndex>=(PageCount-1))
{
//output.WriteLine("<a href=\"{0}\" style='color:black;'>下页</a> ",Page.GetPostBackClientHyperlink(this,CurrentPageIndex.ToString()));
}
else
{
output.WriteLine("<a href=\"{0}\" style='color:#0000FF;'>下页</a> ",Page.GetPostBackClientHyperlink(this,iNextPageN0.ToString()));
}
}
public int Count
{
get
{
if(ViewState["Count"]!=null)
return (int)ViewState["Count"];
else
return 0;
}
set
{
ViewState["Count"] = value;
}
}
public int CurrentPageIndex
{
get
{
if(ViewState["CurrentPageIndex"]!=null)
return (int)ViewState["CurrentPageIndex"];
else
return 0;
}
set
{
ViewState["CurrentPageIndex"] = value;
}
}
public int PageSize
{
get
{
if(ViewState["PageSize"]!=null)
return (int)ViewState["PageSize"];
else
return 20;
}
set
{
ViewState["PageSize"] = value;
}
}
public int PageCount
{
get
{
return (int)Math.Ceiling((double)Count/PageSize);
}
}
public string LinkedPager
{
get
{
return (string)ViewState["LinkedPager"];
}
set
{
ViewState["LinkedPager"] = value;
}
}
IPostBackEventHandler
}
使用:
private void Pager_PageChange(object sender, EventArgs e)
{
BindData();
}
private void BindData()
{
DataSet ds = DB.board_layout(PageBoardID,PageUserID,PageCategoryID,PageForumID);
if(ds.Tables["yaf_Forum"].Rows.Count>0)
{
ForumList.DataSource = ds.Tables["yaf_Forum"].Rows;
SubForums.Visible = true;
}
//一页显示数据行数
Pager.PageSize = 20;
int nCurrentPageIndex = Pager.CurrentPageIndex;
int nPageSize = Pager.PageSize;
DataTable dtTopics;
dtTopics = DB.topic_list(PageForumID,nCurrentPageIndex*nPageSize,nPageSize,PageUserID,OrderType);
int nRowCount = 0;
if(dtTopics.Rows.Count>0) nRowCount = (int)dtTopics.Rows[0]["RowCount"];
int nPageCount = (nRowCount + nPageSize - 1) / nPageSize;
Announcements.DataSource = dtTopics;
DataBind();
//数据有多少页
Pager.Count = nRowCount;
}
/// Summary description for Pager.
/// </summary>
public class Pager : BaseControl, System.Web.UI.IPostBackEventHandler
{
public Pager()
{
this.Load += new EventHandler(Pager_Load);
}
private void Pager_Load(object sender,EventArgs e)
{
}
protected override void Render(HtmlTextWriter output)
{
if(this.PageCount == 0)
{
return;
}
if(LinkedPager!=null)
{
Pager linkedPager = (Pager)Parent.FindControl(LinkedPager);
if(linkedPager==null)
throw new Exception(string.Format("Failed to link pager to '{0}'.",LinkedPager));
linkedPager.Render(output);
return;
}
//显示页次
output.WriteLine("<span>");
output.WriteLine("页次:{0}/{1} ",CurrentPageIndex+1,PageCount);
//跳转
output.WriteLine("<script LANGUAGE='javascript'>");
output.WriteLine("function formHandler()");
output.WriteLine("{ ");
output.WriteLine("var xx = document.getElementById('Select1');");
output.WriteLine("var URL = xx.options[xx.selectedIndex].value; ");
output.WriteLine("window.location.href = URL; ");
output.WriteLine("} ");
output.WriteLine("</script> ");
output.WriteLine("转到第<select NAME='site' SIZE='1' onChange='formHandler()' ID='Select1'>");
for(int i= 0;i<PageCount; i++)
{
if(i== CurrentPageIndex)
output.WriteLine("<option value={0} selected >{1}</option>", Page.GetPostBackClientHyperlink(this,i.ToString()),i+1);
else
output.WriteLine("<option value={0}>{1}</option>", Page.GetPostBackClientHyperlink(this,i.ToString()),i+1);
}
output.WriteLine("</select>页 ");
output.WriteLine("</span>");
//显示上下翻页
int iLastPageN0 = CurrentPageIndex-1;
int iNextPageN0 = CurrentPageIndex+1;
if(CurrentPageIndex<=0)
{
//output.WriteLine("<a href=\"{0}\" style='color:black;'>上页</a>",Page.GetPostBackClientHyperlink(this,CurrentPageIndex.ToString()));
}
else
{
output.WriteLine("<a href=\"{0}\" style='color:#0000FF;'>上页</a>",Page.GetPostBackClientHyperlink(this,iLastPageN0.ToString()));
}
if(CurrentPageIndex>0&CurrentPageIndex<(PageCount-1))
{
output.WriteLine(" | ");
}
if(CurrentPageIndex>=(PageCount-1))
{
//output.WriteLine("<a href=\"{0}\" style='color:black;'>下页</a> ",Page.GetPostBackClientHyperlink(this,CurrentPageIndex.ToString()));
}
else
{
output.WriteLine("<a href=\"{0}\" style='color:#0000FF;'>下页</a> ",Page.GetPostBackClientHyperlink(this,iNextPageN0.ToString()));
}
}
public int Count
{
get
{
if(ViewState["Count"]!=null)
return (int)ViewState["Count"];
else
return 0;
}
set
{
ViewState["Count"] = value;
}
}
public int CurrentPageIndex
{
get
{
if(ViewState["CurrentPageIndex"]!=null)
return (int)ViewState["CurrentPageIndex"];
else
return 0;
}
set
{
ViewState["CurrentPageIndex"] = value;
}
}
public int PageSize
{
get
{
if(ViewState["PageSize"]!=null)
return (int)ViewState["PageSize"];
else
return 20;
}
set
{
ViewState["PageSize"] = value;
}
}
public int PageCount
{
get
{
return (int)Math.Ceiling((double)Count/PageSize);
}
}
public string LinkedPager
{
get
{
return (string)ViewState["LinkedPager"];
}
set
{
ViewState["LinkedPager"] = value;
}
}
IPostBackEventHandler
}
使用:
private void Pager_PageChange(object sender, EventArgs e)
{
BindData();
}
private void BindData()
{
DataSet ds = DB.board_layout(PageBoardID,PageUserID,PageCategoryID,PageForumID);
if(ds.Tables["yaf_Forum"].Rows.Count>0)
{
ForumList.DataSource = ds.Tables["yaf_Forum"].Rows;
SubForums.Visible = true;
}
//一页显示数据行数
Pager.PageSize = 20;
int nCurrentPageIndex = Pager.CurrentPageIndex;
int nPageSize = Pager.PageSize;
DataTable dtTopics;
dtTopics = DB.topic_list(PageForumID,nCurrentPageIndex*nPageSize,nPageSize,PageUserID,OrderType);
int nRowCount = 0;
if(dtTopics.Rows.Count>0) nRowCount = (int)dtTopics.Rows[0]["RowCount"];
int nPageCount = (nRowCount + nPageSize - 1) / nPageSize;
Announcements.DataSource = dtTopics;
DataBind();
//数据有多少页
Pager.Count = nRowCount;
}