100分求ASP.NET (C#)的分页用户控件 200分求ASP.NET (C#)的分页用户控件 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.webdiyer.com/webdiyer/files.asp 就DataGrid就有分页显示的功能设置:AllowPaging="True" //允许分页显示 PagerStyle-NextPageText="下一页" PagerStyle-PrevPageText="上一页" PageSize=10 //每页显示的记录条数 DataGrid分页例子(C#)C#版本DataGridPaging.aspx<%@ Page language="c#" EnableViewState = "true" Codebehind="DataGridPaging.aspx.cs" AutoEventWireup="false" Inherits="eMeng.Exam.DataGridPaging.DataGridPaging" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD><meta content="Visual Basic 7.0" name="CODE_LANGUAGE"><meta content="JavaScript" name="vs_defaultClientScript"><meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"></HEAD><body MS_POSITIONING="GridLayout"><form id="Form1" runat="server"><asp:datagrid id="MyDataGrid" runat="server" AutoGenerateColumns="False" HorizontalAlign="Center" AlternatingItemStyle-BackColor="#eeeeee" HeaderStyle-BackColor="#aaaadd" Font-Size="8pt" Font-Name="Verdana" CellSpacing="0" CellPadding="3" GridLines="Both" BorderWidth="1" BorderColor="black" OnPageIndexChanged="MyDataGrid_Page" PagerStyle-HorizontalAlign="Right" PagerStyle-Mode="NumericPages" PageSize="5" AllowPaging="True"> <AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle> <HeaderStyle BackColor="#AAAADD" Font-Bold="True" HorizontalAlign="Center"></HeaderStyle> <PagerStyle HorizontalAlign="Right" Mode="NumericPages"></PagerStyle> <Columns> <asp:BoundColumn HeaderText="标题" DataField="Title" HeaderStyle-Width="480px"> </asp:BoundColumn> <asp:BoundColumn HeaderText="发表日期" DataField="CreateDate" DataFormatString="{0:yyyy-MM-dd hh:mm:ss}"> </asp:BoundColumn> </Columns></asp:datagrid><p style="FONT-SIZE:9pt" align="center"> <asp:label id="lblPageCount" runat="server"></asp:label> <asp:label id="lblCurrentIndex" runat="server"></asp:label> <asp:linkbutton id="btnFirst" onclick="PagerButtonClick" runat="server" Font-Name="verdana" Font-size="8pt" ForeColor="navy" CommandArgument="0"></asp:linkbutton> <asp:linkbutton id="btnPrev" onclick="PagerButtonClick" runat="server" Font-Name="verdana" Font-size="8pt" ForeColor="navy" CommandArgument="prev"></asp:linkbutton> <asp:linkbutton id="btnNext" onclick="PagerButtonClick" runat="server" Font-Name="verdana" Font-size="8pt" ForeColor="navy" CommandArgument="next"></asp:linkbutton> <asp:linkbutton id="btnLast" onclick="PagerButtonClick" runat="server" Font-Name="verdana" Font-size="8pt" ForeColor="navy" CommandArgument="last"></asp:linkbutton></p></form></body></HTML>DataGridPaging.aspx.csusing System;using System.Collections;using System.ComponentModel;using System.Data;using System.Data.OleDb;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 eMeng.Exam.DataGridPaging{/// <summary>/// DataGridPaging 的摘要说明。/// </summary>public class DataGridPaging : System.Web.UI.Page{ protected System.Web.UI.WebControls.DataGrid MyDataGrid; protected System.Web.UI.WebControls.Label lblPageCount; protected System.Web.UI.WebControls.Label lblCurrentIndex; protected System.Web.UI.WebControls.LinkButton btnFirst; protected System.Web.UI.WebControls.LinkButton btnPrev; protected System.Web.UI.WebControls.LinkButton btnNext; protected System.Web.UI.WebControls.LinkButton btnLast; private OleDbConnection cn = new OleDbConnection();private void Page_Load(object sender, System.EventArgs e){ // 在此处放置用户代码以初始化页面 btnFirst.Text = "最首页"; btnPrev.Text = "前一页"; btnNext.Text = "下一页"; btnLast.Text = "最后页"; OpenDatabase(); BindGrid();}private void OpenDatabase(){ cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("xxxx.mdb"); cn.Open();}private void ShowStats(){ lblCurrentIndex.Text = "第 " + (MyDataGrid.CurrentPageIndex + 1).ToString() + " 页"; lblPageCount.Text = "总共 " + MyDataGrid.PageCount.ToString() + " 页";}public void PagerButtonClick(object sender, EventArgs e){ string arg = ((LinkButton)sender).CommandArgument.ToString(); switch(arg) { case "next": if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1)) { MyDataGrid.CurrentPageIndex += 1; } break; case "prev": if (MyDataGrid.CurrentPageIndex > 0) { MyDataGrid.CurrentPageIndex -= 1; } break; case "last": MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1); break; default: MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg); break; } BindGrid(); ShowStats();}public void BindGrid(){ OleDbConnection myConnection = cn; DataSet ds = new DataSet(); OleDbDataAdapter adapter = new OleDbDataAdapter("Select Title,CreateDate from Document", myConnection); adapter.Fill(ds, "Document"); MyDataGrid.DataSource = ds.Tables["Document"].DefaultView; MyDataGrid.DataBind(); ShowStats();}public void MyDataGrid_Page(object sender, DataGridPageChangedEventArgs e){ int startIndex ; startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize; MyDataGrid.CurrentPageIndex = e.NewPageIndex; BindGrid(); ShowStats();}#region Web Form Designer generated codeoverride protected void OnInit(EventArgs e){//// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。//InitializeComponent();base.OnInit(e);}/// <summary>/// 设计器支持所需的方法 - 不要使用代码编辑器修改/// 此方法的内容。/// </summary>private void InitializeComponent(){this.Load += new System.EventHandler(this.Page_Load);}#endregion}} 以下是我现在用的控件源码using System;using System.Web.UI;using System.Web.UI.WebControls;using System.ComponentModel;namespace HFSoft.Web.WebControls{ [Serializable] public class ChangePageArgs:EventArgs { public ChangePageArgs() { } private int _PageIndex; public int PageIndex { get { return _PageIndex; } set { _PageIndex = value; } } private int _PageSize; public int PageSize { get { return _PageSize; } set { _PageSize = value; } } } /// <summary> /// 更改页数委托 /// </summary> public delegate void EventChangePageIndex(object sender,ChangePageArgs e ); public class PaginationBar : System.Web.UI.WebControls.WebControl,System.Web.UI.INamingContainer { public event EventChangePageIndex ChangePageIndex; public ChangePageArgs PageInfo { get { return ViewState["PageInfo"] as ChangePageArgs; } set { ViewState["PageInfo"] = value; } } protected virtual void OnChangePageIndex(ChangePageArgs e) { PageInfo = e; if(ChangePageIndex != null) ChangePageIndex(this,e); } private DropDownList c_PageIndex; private DropDownList c_PageSize ; private LinkButton c_First ; private LinkButton c_Last ; private LinkButton c_Next ; private LinkButton c_Previ ; public int RecordCount { get { if(ViewState["RecordCount"] == null) return 0; return (int)ViewState["RecordCount"]; } set { ViewState["RecordCount"] = value; DefaultPageInfo(); } } public int PageIndex { get { if(ViewState["PageIndex"] == null) return 1; return (int)ViewState["PageIndex"]; } set { ViewState["PageIndex"] = value; } } protected int PageCount { get { if(ViewState["PageCount"] == null) return 0; return (int)ViewState["PageCount"]; } set { ViewState["PageCount"] = value; } } protected int PageSize { get { if(ViewState["PageSize"] == null) return 10; return (int)ViewState["PageSize"]; } set { ViewState["PageSize"] = value; this.PageIndex = 1; DefaultPageInfo(); } } public void Open() { CellEvent(); } private void DefaultPageInfo() { //PageIndex = 1; if(PageSize > 0) { PageCount = (int)(RecordCount%this.PageSize>0?Math.Floor( RecordCount/this.PageSize)+1 :Math.Floor(RecordCount/this.PageSize)); } else { PageIndex = 1; PageCount = 0; } //CreateControl(); } private void CellEvent() { ChangePageArgs e = new ChangePageArgs(); e.PageSize = this.PageSize; e.PageIndex = this.PageIndex; OnChangePageIndex(e); //CreateControl(); } /// <summary> /// 将此控件呈现给指定的输出参数。 /// </summary> /// <param name="output"> 要写出到的 HTML 编写器 </param> protected override void Render(System.Web.UI.HtmlTextWriter output) { CreateChildControls(); base.Render(output); } public void Refresh() { CellEvent(); } protected override void CreateChildControls() { base.CreateChildControls (); CreateControl(); } private void CreateControl() { Controls.Clear(); c_PageSize = new DropDownList(); c_PageSize.ID = "_PageSize"; c_PageSize.AutoPostBack = true; c_PageIndex = new DropDownList(); c_PageIndex.ID = "_PageIndex"; c_PageIndex.AutoPostBack = true; c_PageIndex.SelectedIndexChanged += new EventHandler(this.PageIndex111_SelectedIndexChanged); c_PageSize.SelectedIndexChanged += new EventHandler(this.PageSize_SelectedIndexChanged); c_PageSize.Items.Add(new ListItem("10","10")); c_PageSize.Items.Add(new ListItem("15","15")); c_PageSize.Items.Add(new ListItem("20","20")); c_PageSize.Items.Add(new ListItem("30","30")); c_PageSize.Items.Add(new ListItem("50","50")); c_PageSize.Items.Add(new ListItem("全部","0")); for(int i =0;i< c_PageSize.Items.Count;i++) { if(PageSize.ToString() == c_PageSize.Items[i].Value) { c_PageSize.SelectedIndex = i; } } if(PageCount > 0) { for(int i=1;i<= this.PageCount;i++) { c_PageIndex.Items.Add(new ListItem(i.ToString(),i.ToString())); } c_PageIndex.SelectedIndex = this.PageIndex -1; } c_First = new LinkButton(); c_First.ID = "t3"; c_First.Click += new EventHandler(this.First_Click); c_First.CssClass = this.CssClass; c_First.Text = "首页"; c_Last = new LinkButton(); c_Last.ID = "T4"; c_Last.Click += new EventHandler(this.Last_Click); c_Last.CssClass = this.CssClass; c_Last.Text = "尾页"; c_Next = new LinkButton(); c_Next.ID = "T5"; c_Next.Click += new EventHandler(this.Next_Click); c_Next.CssClass = this.CssClass; c_Next.Text = "下一页"; c_Previ = new LinkButton(); c_Previ.ID = "T6"; c_Previ.Click += new EventHandler(this.Previ_Click); c_Previ.CssClass = this.CssClass; c_Previ.Text = "上一页"; Table tbl = new Table(); setControlStyle(c_First,c_Last,c_Next,c_Previ,tbl); tbl.CssClass = this.CssClass; tbl.Width = Unit.Parse("100%"); TableRow row = new TableRow(); row.Width = Unit.Parse("100%"); int cellIndex = 0; cellIndex = row.Cells.Add(new TableCell()); row.Cells[cellIndex].Text = "页数:"; cellIndex = row.Cells.Add(new TableCell()); row.Cells[cellIndex].Controls.Add(c_PageIndex); cellIndex = row.Cells.Add(new TableCell()); row.Cells[cellIndex].Text = "共" + this.PageCount + "页"; cellIndex = row.Cells.Add(new TableCell()); row.Cells[cellIndex].Width=Unit.Parse("10"); cellIndex = row.Cells.Add(new TableCell()); row.Cells[cellIndex].Text = "分页记录数"; cellIndex = row.Cells.Add(new TableCell()); row.Cells[cellIndex].Controls.Add( c_PageSize); cellIndex = row.Cells.Add(new TableCell()); row.Cells[cellIndex].Width=Unit.Parse("10"); cellIndex = row.Cells.Add(new TableCell()); row.Cells[cellIndex].Controls.Add(c_First); cellIndex = row.Cells.Add(new TableCell()); row.Cells[cellIndex].Controls.Add(c_Previ); cellIndex = row.Cells.Add(new TableCell()); row.Cells[cellIndex].Controls.Add(c_Next); cellIndex = row.Cells.Add(new TableCell()); row.Cells[cellIndex].Controls.Add(c_Last); tbl.Rows.Add(row); if(this.PageIndex ==1) { c_First.Enabled = false; c_Previ.Enabled = false; } if(this.PageIndex == this.PageCount) { c_Last.Enabled = false; c_Next.Enabled = false; } if(PageSize== 0 || this.RecordCount ==0) { c_First.Enabled = false; c_Previ.Enabled = false; c_Last.Enabled = false; c_Next.Enabled = false; c_PageIndex.Enabled = false; } this.Controls.Add(tbl); } private void Next_Click(object sender, System.EventArgs e) { PageIndex = PageIndex +1; CellEvent(); } private void Previ_Click(object sender, System.EventArgs e) { PageIndex = PageIndex - 1; CellEvent(); } private void First_Click(object sender, System.EventArgs e) { PageIndex =1; CellEvent(); } private void Last_Click(object sender, System.EventArgs e) { PageIndex = PageCount; CellEvent(); } private void PageIndex111_SelectedIndexChanged(object sender, System.EventArgs e) { this.PageIndex = int.Parse(c_PageIndex.SelectedItem.Value); CellEvent(); } private void PageSize_SelectedIndexChanged(object sender, System.EventArgs e) { this.PageSize = int.Parse(c_PageSize.SelectedItem.Value); CellEvent(); } private void setControlStyle(params System.Web.UI.WebControls.WebControl[] pControls) { foreach(WebControl item in pControls) { item.CssClass = this.CssClass; item.ForeColor = this.ForeColor; item.BackColor = this.BackColor; } } public System.Data.DataView FilterData(System.Data.DataTable pTable,ChangePageArgs e) { System.Data.DataView dv = new System.Data.DataView(pTable); int rowCount = dv.Count; if(e.PageSize ==0) return dv; int startRow = (e.PageIndex-1) * e.PageSize; int endRow = startRow + e.PageSize ; for(int i = startRow ;i< endRow;i++) { if(i >= pTable.Rows.Count ) break; pTable.Rows[i].Delete(); } dv.RowStateFilter = System.Data.DataViewRowState.Deleted; return dv; } }} public PagedDataSource databind(DataTable dt) { this.lb_url.Text = url; this.lb_Params.Text = Params; PagedDataSource objPage = new PagedDataSource(); objPage.DataSource = dt.DefaultView; objPage.AllowPaging = true; objPage.PageSize = pagesize; //设置当前页的索引 if (Request.QueryString["Page"] != null) { CurPage = Int32.Parse(Request.QueryString["Page"]); Session["Page"] = CurPage.ToString(); } else CurPage = 1; this.hpl_Last.NavigateUrl = url + "?Page=" + objPage.PageCount.ToString(); objPage.CurrentPageIndex = CurPage-1; //显示状态信息 lb_ItemCount.Text = dt.Rows.Count.ToString(); lb_CurrentPage.Text = CurPage.ToString(); lb_PageCount.Text =objPage.PageCount.ToString(); //如果当前页面不是首页 if (!objPage.IsFirstPage) { hpl_Prev.NavigateUrl=url + "?Page=" + Convert.ToString(CurPage-1)+Params; hpl_First.NavigateUrl=url + "?Page=1"+Params; } //如果当前页面不是最后一页 if (!objPage.IsLastPage) { hpl_Next.NavigateUrl=url+ "?Page=" + Convert.ToString(CurPage+1)+Params; hpl_Last.NavigateUrl=url + "?Page=" +objPage.PageCount.ToString()+Params; } return objPage; }这是我写在用户控件中的分页代码,下面是前台调用的代码:AutoPage pi = (AutoPage)this.LoadControl("AutoPage.ascx");//导入用户控件 pi.pagesize = 10; pi.PageP = Request.QueryString["Page"]; pi.Params = "1"; pi.url = "PageDataSourceUI.aspx"; this.DataList1.DataSource = pi.databind(Db_class.dataTable("SELECT * FROM 客户")); this.DataList1.DataBind();但就是PagedDataSource中的一些值取不到,上一页,下一页没用 www.369sky.com上有一个通用的分页控件,以前的只支持 sql 和oracle,我改了一下,现在也支持access数据库 JS链接问题 asp.net C#怎么实现多文件上传 求radiobuttonlist的用法: 例如说,点击读者就进入读者应用的界面,点击管理员进入管理员应用界面 Repeater中实现全选 formview控件中如何显示下一个记录? 请问如何在服务器端向客户端发送一个命令来清除客户端的HTML码呢? 安装IE Web控件 dropdownlist客户端填充之后怎样在服务器端获得数据?? 请问SqlHelper.ExecuteScalar的详细用法?谢谢 HTML背景图片怎样依浏览器伸缩放大 asp.net分层的写法 有什么方法可以把全局的Response内容REPLACE过滤后再输出?
设置:AllowPaging="True" //允许分页显示
PagerStyle-NextPageText="下一页"
PagerStyle-PrevPageText="上一页"
PageSize=10 //每页显示的记录条数
DataGridPaging.aspx
<%@ Page language="c#" EnableViewState = "true" Codebehind="DataGridPaging.aspx.cs"
AutoEventWireup="false" Inherits="eMeng.Exam.DataGridPaging.DataGridPaging" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" runat="server">
<asp:datagrid id="MyDataGrid" runat="server" AutoGenerateColumns="False"
HorizontalAlign="Center" AlternatingItemStyle-BackColor="#eeeeee"
HeaderStyle-BackColor="#aaaadd" Font-Size="8pt" Font-Name="Verdana"
CellSpacing="0" CellPadding="3" GridLines="Both" BorderWidth="1"
BorderColor="black" OnPageIndexChanged="MyDataGrid_Page" PagerStyle-HorizontalAlign="Right"
PagerStyle-Mode="NumericPages" PageSize="5" AllowPaging="True">
<AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle>
<HeaderStyle BackColor="#AAAADD" Font-Bold="True" HorizontalAlign="Center"></HeaderStyle>
<PagerStyle HorizontalAlign="Right" Mode="NumericPages"></PagerStyle>
<Columns>
<asp:BoundColumn HeaderText="标题" DataField="Title" HeaderStyle-Width="480px">
</asp:BoundColumn>
<asp:BoundColumn HeaderText="发表日期" DataField="CreateDate" DataFormatString="{0:yyyy-MM-dd hh:mm:ss}">
</asp:BoundColumn>
</Columns>
</asp:datagrid>
<p style="FONT-SIZE:9pt" align="center">
<asp:label id="lblPageCount" runat="server"></asp:label>
<asp:label id="lblCurrentIndex" runat="server"></asp:label>
<asp:linkbutton id="btnFirst" onclick="PagerButtonClick" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="0"></asp:linkbutton>
<asp:linkbutton id="btnPrev" onclick="PagerButtonClick" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="prev"></asp:linkbutton>
<asp:linkbutton id="btnNext" onclick="PagerButtonClick" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="next"></asp:linkbutton>
<asp:linkbutton id="btnLast" onclick="PagerButtonClick" runat="server" Font-Name="verdana"
Font-size="8pt" ForeColor="navy" CommandArgument="last"></asp:linkbutton>
</p>
</form>
</body>
</HTML>
DataGridPaging.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
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 eMeng.Exam.DataGridPaging
{
/// <summary>
/// DataGridPaging 的摘要说明。
/// </summary>
public class DataGridPaging : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
protected System.Web.UI.WebControls.Label lblPageCount;
protected System.Web.UI.WebControls.Label lblCurrentIndex;
protected System.Web.UI.WebControls.LinkButton btnFirst;
protected System.Web.UI.WebControls.LinkButton btnPrev;
protected System.Web.UI.WebControls.LinkButton btnNext;
protected System.Web.UI.WebControls.LinkButton btnLast;
private OleDbConnection cn = new OleDbConnection();
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
btnFirst.Text = "最首页";
btnPrev.Text = "前一页";
btnNext.Text = "下一页";
btnLast.Text = "最后页";
OpenDatabase();
BindGrid();
}
private void OpenDatabase()
{
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("xxxx.mdb");
cn.Open();
}
private void ShowStats()
{
lblCurrentIndex.Text = "第 " + (MyDataGrid.CurrentPageIndex + 1).ToString() + " 页";
lblPageCount.Text = "总共 " + MyDataGrid.PageCount.ToString() + " 页";
}
public void PagerButtonClick(object sender, EventArgs e)
{
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch(arg)
{
case "next":
if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
{
MyDataGrid.CurrentPageIndex += 1;
}
break;
case "prev":
if (MyDataGrid.CurrentPageIndex > 0)
{
MyDataGrid.CurrentPageIndex -= 1;
}
break;
case "last":
MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
break;
default:
MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg);
break;
}
BindGrid();
ShowStats();
}
public void BindGrid()
{
OleDbConnection myConnection = cn;
DataSet ds = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter("Select Title,CreateDate from Document", myConnection);
adapter.Fill(ds, "Document");
MyDataGrid.DataSource = ds.Tables["Document"].DefaultView;
MyDataGrid.DataBind();
ShowStats();
}
public void MyDataGrid_Page(object sender, DataGridPageChangedEventArgs e)
{
int startIndex ;
startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize;
MyDataGrid.CurrentPageIndex = e.NewPageIndex;
BindGrid();
ShowStats();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace HFSoft.Web.WebControls
{
[Serializable]
public class ChangePageArgs:EventArgs
{
public ChangePageArgs()
{
}
private int _PageIndex;
public int PageIndex
{
get
{
return _PageIndex;
}
set
{
_PageIndex = value;
}
}
private int _PageSize;
public int PageSize
{
get
{
return _PageSize;
}
set
{
_PageSize = value;
}
}
}
/// <summary>
/// 更改页数委托
/// </summary>
public delegate void EventChangePageIndex(object sender,ChangePageArgs e );
public class PaginationBar : System.Web.UI.WebControls.WebControl,System.Web.UI.INamingContainer
{
public event EventChangePageIndex ChangePageIndex;
public ChangePageArgs PageInfo
{
get
{
return ViewState["PageInfo"] as ChangePageArgs;
}
set
{
ViewState["PageInfo"] = value;
}
}
protected virtual void OnChangePageIndex(ChangePageArgs e)
{
PageInfo = e;
if(ChangePageIndex != null)
ChangePageIndex(this,e);
}
private DropDownList c_PageIndex;
private DropDownList c_PageSize ;
private LinkButton c_First ;
private LinkButton c_Last ;
private LinkButton c_Next ;
private LinkButton c_Previ ;
public int RecordCount
{
get
{
if(ViewState["RecordCount"] == null)
return 0;
return (int)ViewState["RecordCount"];
}
set
{
ViewState["RecordCount"] = value;
DefaultPageInfo();
}
}
public int PageIndex
{
get
{
if(ViewState["PageIndex"] == null)
return 1;
return (int)ViewState["PageIndex"];
}
set
{
ViewState["PageIndex"] = value;
}
}
protected int PageCount
{
get
{
if(ViewState["PageCount"] == null)
return 0;
return (int)ViewState["PageCount"];
}
set
{
ViewState["PageCount"] = value;
}
}
protected int PageSize
{
get
{
if(ViewState["PageSize"] == null)
return 10;
return (int)ViewState["PageSize"];
}
set
{
ViewState["PageSize"] = value;
this.PageIndex = 1;
DefaultPageInfo();
}
}
public void Open()
{
CellEvent();
}
private void DefaultPageInfo()
{
//PageIndex = 1;
if(PageSize > 0)
{
PageCount = (int)(RecordCount%this.PageSize>0?Math.Floor(
RecordCount/this.PageSize)+1
:Math.Floor(RecordCount/this.PageSize));
}
else
{
PageIndex = 1;
PageCount = 0;
}
//CreateControl();
}
private void CellEvent()
{
ChangePageArgs e = new ChangePageArgs();
e.PageSize = this.PageSize;
e.PageIndex = this.PageIndex;
OnChangePageIndex(e);
//CreateControl();
}
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
protected override void Render(System.Web.UI.HtmlTextWriter output)
{
CreateChildControls();
base.Render(output);
}
public void Refresh()
{
CellEvent();
}
protected override void CreateChildControls()
{
base.CreateChildControls ();
CreateControl();
}
{
Controls.Clear();
c_PageSize = new DropDownList();
c_PageSize.ID = "_PageSize";
c_PageSize.AutoPostBack = true;
c_PageIndex = new DropDownList();
c_PageIndex.ID = "_PageIndex";
c_PageIndex.AutoPostBack = true;
c_PageIndex.SelectedIndexChanged += new EventHandler(this.PageIndex111_SelectedIndexChanged);
c_PageSize.SelectedIndexChanged += new EventHandler(this.PageSize_SelectedIndexChanged);
c_PageSize.Items.Add(new ListItem("10","10"));
c_PageSize.Items.Add(new ListItem("15","15"));
c_PageSize.Items.Add(new ListItem("20","20"));
c_PageSize.Items.Add(new ListItem("30","30"));
c_PageSize.Items.Add(new ListItem("50","50"));
c_PageSize.Items.Add(new ListItem("全部","0"));
for(int i =0;i< c_PageSize.Items.Count;i++)
{
if(PageSize.ToString() == c_PageSize.Items[i].Value)
{
c_PageSize.SelectedIndex = i;
}
}
if(PageCount > 0)
{
for(int i=1;i<= this.PageCount;i++)
{
c_PageIndex.Items.Add(new ListItem(i.ToString(),i.ToString()));
}
c_PageIndex.SelectedIndex = this.PageIndex -1;
}
c_First = new LinkButton();
c_First.ID = "t3";
c_First.Click += new EventHandler(this.First_Click);
c_First.CssClass = this.CssClass;
c_First.Text = "首页";
c_Last = new LinkButton();
c_Last.ID = "T4";
c_Last.Click += new EventHandler(this.Last_Click);
c_Last.CssClass = this.CssClass;
c_Last.Text = "尾页";
c_Next = new LinkButton();
c_Next.ID = "T5";
c_Next.Click += new EventHandler(this.Next_Click);
c_Next.CssClass = this.CssClass;
c_Next.Text = "下一页";
c_Previ = new LinkButton();
c_Previ.ID = "T6";
c_Previ.Click += new EventHandler(this.Previ_Click);
c_Previ.CssClass = this.CssClass;
c_Previ.Text = "上一页";
Table tbl = new Table();
setControlStyle(c_First,c_Last,c_Next,c_Previ,tbl);
tbl.CssClass = this.CssClass;
tbl.Width = Unit.Parse("100%");
TableRow row = new TableRow();
row.Width = Unit.Parse("100%");
int cellIndex = 0;
cellIndex = row.Cells.Add(new TableCell());
row.Cells[cellIndex].Text = "页数:";
cellIndex = row.Cells.Add(new TableCell());
row.Cells[cellIndex].Controls.Add(c_PageIndex);
cellIndex = row.Cells.Add(new TableCell());
row.Cells[cellIndex].Text = "共" + this.PageCount + "页";
cellIndex = row.Cells.Add(new TableCell());
row.Cells[cellIndex].Width=Unit.Parse("10");
cellIndex = row.Cells.Add(new TableCell());
row.Cells[cellIndex].Text = "分页记录数";
cellIndex = row.Cells.Add(new TableCell());
row.Cells[cellIndex].Controls.Add( c_PageSize);
cellIndex = row.Cells.Add(new TableCell());
row.Cells[cellIndex].Width=Unit.Parse("10");
cellIndex = row.Cells.Add(new TableCell());
row.Cells[cellIndex].Controls.Add(c_First);
cellIndex = row.Cells.Add(new TableCell());
row.Cells[cellIndex].Controls.Add(c_Previ);
cellIndex = row.Cells.Add(new TableCell());
row.Cells[cellIndex].Controls.Add(c_Next);
cellIndex = row.Cells.Add(new TableCell());
row.Cells[cellIndex].Controls.Add(c_Last);
tbl.Rows.Add(row);
if(this.PageIndex ==1)
{
c_First.Enabled = false;
c_Previ.Enabled = false;
}
if(this.PageIndex == this.PageCount)
{
c_Last.Enabled = false;
c_Next.Enabled = false;
}
if(PageSize== 0 || this.RecordCount ==0)
{
c_First.Enabled = false;
c_Previ.Enabled = false;
c_Last.Enabled = false;
c_Next.Enabled = false;
c_PageIndex.Enabled = false;
}
this.Controls.Add(tbl);
}
private void Next_Click(object sender, System.EventArgs e)
{
PageIndex = PageIndex +1;
CellEvent();
}
private void Previ_Click(object sender, System.EventArgs e)
{
PageIndex = PageIndex - 1;
CellEvent();
}
private void First_Click(object sender, System.EventArgs e)
{
PageIndex =1;
CellEvent();
}
private void Last_Click(object sender, System.EventArgs e)
{
PageIndex = PageCount;
CellEvent();
}
private void PageIndex111_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.PageIndex = int.Parse(c_PageIndex.SelectedItem.Value);
CellEvent();
}
private void PageSize_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.PageSize = int.Parse(c_PageSize.SelectedItem.Value);
CellEvent();
}
private void setControlStyle(params System.Web.UI.WebControls.WebControl[] pControls)
{
foreach(WebControl item in pControls)
{
item.CssClass = this.CssClass;
item.ForeColor = this.ForeColor;
item.BackColor = this.BackColor;
}
}
public System.Data.DataView FilterData(System.Data.DataTable pTable,ChangePageArgs e)
{
System.Data.DataView dv = new System.Data.DataView(pTable);
int rowCount = dv.Count;
if(e.PageSize ==0)
return dv;
int startRow = (e.PageIndex-1) * e.PageSize;
int endRow = startRow + e.PageSize ;
for(int i = startRow ;i< endRow;i++)
{
if(i >= pTable.Rows.Count )
break;
pTable.Rows[i].Delete();
}
dv.RowStateFilter = System.Data.DataViewRowState.Deleted;
return dv;
}
}
}
{
this.lb_url.Text = url;
this.lb_Params.Text = Params;
PagedDataSource objPage = new PagedDataSource();
objPage.DataSource = dt.DefaultView;
objPage.AllowPaging = true;
objPage.PageSize = pagesize; //设置当前页的索引
if (Request.QueryString["Page"] != null)
{
CurPage = Int32.Parse(Request.QueryString["Page"]);
Session["Page"] = CurPage.ToString();
}
else
CurPage = 1; this.hpl_Last.NavigateUrl = url + "?Page=" + objPage.PageCount.ToString();
objPage.CurrentPageIndex = CurPage-1;
//显示状态信息
lb_ItemCount.Text = dt.Rows.Count.ToString();
lb_CurrentPage.Text = CurPage.ToString(); lb_PageCount.Text =objPage.PageCount.ToString();
//如果当前页面不是首页
if (!objPage.IsFirstPage)
{
hpl_Prev.NavigateUrl=url + "?Page=" + Convert.ToString(CurPage-1)+Params;
hpl_First.NavigateUrl=url + "?Page=1"+Params;
}
//如果当前页面不是最后一页
if (!objPage.IsLastPage)
{
hpl_Next.NavigateUrl=url+ "?Page=" + Convert.ToString(CurPage+1)+Params;
hpl_Last.NavigateUrl=url + "?Page=" +objPage.PageCount.ToString()+Params;
} return objPage; }
这是我写在用户控件中的分页代码,下面是前台调用的代码:AutoPage pi = (AutoPage)this.LoadControl("AutoPage.ascx");//导入用户控件
pi.pagesize = 10;
pi.PageP = Request.QueryString["Page"];
pi.Params = "1";
pi.url = "PageDataSourceUI.aspx";
this.DataList1.DataSource = pi.databind(Db_class.dataTable("SELECT * FROM 客户"));
this.DataList1.DataBind();
但就是PagedDataSource中的一些值取不到,上一页,下一页没用