实现数字分页
1<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="DataGridPage.WebForm1" %>
2<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
3<HTML>
4 <HEAD>
5 <title>WebForm1</title>
6 <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
7 <meta content="C#" name="CODE_LANGUAGE">
8 <meta content="JavaScript" name="vs_defaultClientScript">
9 <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
10 </HEAD>
11 <body MS_POSITIONING="GridLayout">
12 <form id="Form1" method="post" runat="server">
13 <table cellSpacing="0" cellPadding="0" align="center" border="1">
14 <tr>
15 <td><asp:datagrid id="DataGrid1" runat="server" AllowPaging="True" Width="502px" PageSize="3">
16 <PagerStyle NextPageText="" PrevPageText=""></PagerStyle>
17 </asp:datagrid></td>
18 </tr>
19 <tr>
20 <td style="HEIGHT: 25px" align="center"><asp:linkbutton id="btnFirst" onclick="PagerButtonClick" runat="server" CommandArgument="first">首 页</asp:linkbutton><asp:linkbutton id="btnPrev" onclick="PagerButtonClick" runat="server" CommandArgument="prev">上一页 </asp:linkbutton><asp:linkbutton id="btnNext" onclick="PagerButtonClick" runat="server" CommandArgument="next">下一页</asp:linkbutton><asp:linkbutton id="btnLast" onclick="PagerButtonClick" runat="server" CommandArgument="last">尾 页</asp:linkbutton><asp:label id="LblCurrentIndex" runat="server"></asp:label><asp:label id="LblPageCount" runat="server"></asp:label><asp:label id="LblRecordCount" runat="server"></asp:label></td>
21 </tr>
22 <tr>
23 <td>
24 <div style="FLOAT:left;WIDTH:100%;HEIGHT:20px" runat="server" id="div1"></div>
25 </td>
26 </tr>
27 </table>
28 </form>
29 </body>
30</HTML>
31
2<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
3<HTML>
4 <HEAD>
5 <title>WebForm1</title>
6 <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
7 <meta content="C#" name="CODE_LANGUAGE">
8 <meta content="JavaScript" name="vs_defaultClientScript">
9 <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
10 </HEAD>
11 <body MS_POSITIONING="GridLayout">
12 <form id="Form1" method="post" runat="server">
13 <table cellSpacing="0" cellPadding="0" align="center" border="1">
14 <tr>
15 <td><asp:datagrid id="DataGrid1" runat="server" AllowPaging="True" Width="502px" PageSize="3">
16 <PagerStyle NextPageText="" PrevPageText=""></PagerStyle>
17 </asp:datagrid></td>
18 </tr>
19 <tr>
20 <td style="HEIGHT: 25px" align="center"><asp:linkbutton id="btnFirst" onclick="PagerButtonClick" runat="server" CommandArgument="first">首 页</asp:linkbutton><asp:linkbutton id="btnPrev" onclick="PagerButtonClick" runat="server" CommandArgument="prev">上一页 </asp:linkbutton><asp:linkbutton id="btnNext" onclick="PagerButtonClick" runat="server" CommandArgument="next">下一页</asp:linkbutton><asp:linkbutton id="btnLast" onclick="PagerButtonClick" runat="server" CommandArgument="last">尾 页</asp:linkbutton><asp:label id="LblCurrentIndex" runat="server"></asp:label><asp:label id="LblPageCount" runat="server"></asp:label><asp:label id="LblRecordCount" runat="server"></asp:label></td>
21 </tr>
22 <tr>
23 <td>
24 <div style="FLOAT:left;WIDTH:100%;HEIGHT:20px" runat="server" id="div1"></div>
25 </td>
26 </tr>
27 </table>
28 </form>
29 </body>
30</HTML>
31
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace DataGridPage
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
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;
protected System.Web.UI.WebControls.Label LblCurrentIndex;
protected System.Web.UI.WebControls.Label LblPageCount;
protected System.Web.UI.WebControls.Label LblRecordCount;
protected System.Web.UI.HtmlControls.HtmlGenericControl div1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(! this.IsPostBack)
{
string page=Request["page"];
if(page != null)
{
this.DataGrid1.CurrentPageIndex=Convert.ToInt32(Request["page"].ToString())-1;
bind();
}
else
{
bind();
}
}
}
void bind()
{
SqlConnection conn=new SqlConnection("server=.;database=pubs;uid=sa;pwd=;");
conn.Open();
SqlDataAdapter da=new SqlDataAdapter("select * from employee",conn);
DataSet ds=new DataSet();
da.Fill(ds);
this.DataGrid1.DataSource=ds.Tables[0];
this.DataGrid1.DataBind();
this.LblCurrentIndex.Text="第"+(this.DataGrid1.CurrentPageIndex+1)+"页";
this.LblPageCount.Text="共:"+this.DataGrid1.PageCount.ToString()+"页";
this.LblRecordCount.Text="总共"+ds.Tables[0].Rows.Count.ToString()+"条";
if(ds.Tables[0].Rows.Count==0)
{
this.btnFirst.Visible=false;
this.btnPrev.Visible=false;
this.btnNext.Visible=false;
this.btnLast.Visible=false;
this.LblCurrentIndex.Visible=false;
this.LblPageCount.Visible=false;
this.LblRecordCount.Visible=false;
}
else
if(this.DataGrid1.PageCount ==1)
{
this.btnFirst.Visible=false;
this.btnPrev.Visible=false;
this.btnNext.Visible=false;
this.btnLast.Visible=false;
}
btnFirst.CommandName = "1";
btnPrev.CommandName = (this.DataGrid1.CurrentPageIndex == 0 ? "1" : this.DataGrid1.CurrentPageIndex.ToString());
btnNext.CommandName = (this.DataGrid1.PageCount ==1 ? this.DataGrid1.PageCount.ToString() : (this.DataGrid1.CurrentPageIndex + 2).ToString());
btnLast.CommandName =this.DataGrid1.PageCount.ToString();
this.div1.InnerHtml=set_daohang();
}
Web 窗体设计器生成的代码
protected void PagerButtonClick(object sender,EventArgs e)
{
if(Convert.ToInt32(((LinkButton)sender).CommandName) > this.DataGrid1.PageCount)
{
((LinkButton)sender).CommandName=this.DataGrid1.PageCount.ToString();
}
this.DataGrid1.CurrentPageIndex=Convert.ToInt32(((LinkButton)sender).CommandName)-1;
bind();
}
protected string set_daohang()
{
string daohang1=null;
//Response.Write(this.DataGrid1.CurrentPageIndex.ToString());
int l=(this.DataGrid1.CurrentPageIndex+1)/10;
string fenye;
for(int i=l*10+1;i<=(l+1)*10 && i < this.DataGrid1.PageCount+1;i++)
{
if(i==this.DataGrid1.CurrentPageIndex+1)
fenye="<FONT color='red'>"+i+"</FONT>";
else
fenye="<FONT color='#0000ff'>"+i+"</FONT>";
daohang1 +=" <A href='WebForm1.aspx?page="+i+"'><font color='#0000ff' >"+fenye+"</font></a>";
}
return daohang1;
}
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace DataGridPage
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
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;
protected System.Web.UI.WebControls.Label LblCurrentIndex;
protected System.Web.UI.WebControls.Label LblPageCount;
protected System.Web.UI.WebControls.Label LblRecordCount;
protected System.Web.UI.HtmlControls.HtmlGenericControl div1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(! this.IsPostBack)
{
string page=Request["page"];
if(page != null)
{
this.DataGrid1.CurrentPageIndex=Convert.ToInt32(Request["page"].ToString())-1;
bind();
}
else
{
bind();
}
}
}
void bind()
{
SqlConnection conn=new SqlConnection("server=.;database=pubs;uid=sa;pwd=;");
conn.Open();
SqlDataAdapter da=new SqlDataAdapter("select * from employee",conn);
DataSet ds=new DataSet();
da.Fill(ds);
this.DataGrid1.DataSource=ds.Tables[0];
this.DataGrid1.DataBind();
this.LblCurrentIndex.Text="第"+(this.DataGrid1.CurrentPageIndex+1)+"页";
this.LblPageCount.Text="共:"+this.DataGrid1.PageCount.ToString()+"页";
this.LblRecordCount.Text="总共"+ds.Tables[0].Rows.Count.ToString()+"条";
if(ds.Tables[0].Rows.Count==0)
{
this.btnFirst.Visible=false;
this.btnPrev.Visible=false;
this.btnNext.Visible=false;
this.btnLast.Visible=false;
this.LblCurrentIndex.Visible=false;
this.LblPageCount.Visible=false;
this.LblRecordCount.Visible=false;
}
else
if(this.DataGrid1.PageCount ==1)
{
this.btnFirst.Visible=false;
this.btnPrev.Visible=false;
this.btnNext.Visible=false;
this.btnLast.Visible=false;
}
btnFirst.CommandName = "1";
btnPrev.CommandName = (this.DataGrid1.CurrentPageIndex == 0 ? "1" : this.DataGrid1.CurrentPageIndex.ToString());
btnNext.CommandName = (this.DataGrid1.PageCount ==1 ? this.DataGrid1.PageCount.ToString() : (this.DataGrid1.CurrentPageIndex + 2).ToString());
btnLast.CommandName =this.DataGrid1.PageCount.ToString();
this.div1.InnerHtml=set_daohang();
}
Web 窗体设计器生成的代码
protected void PagerButtonClick(object sender,EventArgs e)
{
if(Convert.ToInt32(((LinkButton)sender).CommandName) > this.DataGrid1.PageCount)
{
((LinkButton)sender).CommandName=this.DataGrid1.PageCount.ToString();
}
this.DataGrid1.CurrentPageIndex=Convert.ToInt32(((LinkButton)sender).CommandName)-1;
bind();
}
protected string set_daohang()
{
string daohang1=null;
//Response.Write(this.DataGrid1.CurrentPageIndex.ToString());
int l=(this.DataGrid1.CurrentPageIndex+1)/10;
string fenye;
for(int i=l*10+1;i<=(l+1)*10 && i < this.DataGrid1.PageCount+1;i++)
{
if(i==this.DataGrid1.CurrentPageIndex+1)
fenye="<FONT color='red'>"+i+"</FONT>";
else
fenye="<FONT color='#0000ff'>"+i+"</FONT>";
daohang1 +=" <A href='WebForm1.aspx?page="+i+"'><font color='#0000ff' >"+fenye+"</font></a>";
}
return daohang1;
}
}
}