1、前台在GridView里写实现分页
<PagerTemplate>
<table width="100%">
<tr>
<td style="text-align: right">第
<asp:Label ID="Lab_CurrentPage" runat="server"
Text="<%#((GridView)Container.NamingContainer).PageIndex + 1 %>">
</asp:Label>页/共
<asp:Label ID="Lab_PageCount" runat="server"
Text="<%# ((GridView)Container.NamingContainer).PageCount %>">
</asp:Label>页
<asp:LinkButton ID="LBtn_FirstPage" runat="server" CommandArgument="First"
CommandName="Page" Visible="<%# ((GridView)Container.NamingContainer).PageIndex
!= 0 %>">首页
</asp:LinkButton>
<asp:LinkButton ID="LBtn_PreviousPage" runat="server" CommandArgument="Prev"
CommandName="Page" Visible="<%# ((GridView)Container.NamingContainer).PageIndex
!= 0 %>">上一页
</asp:LinkButton>
<asp:LinkButton ID="LBtn_NextPage" runat="server" CommandArgument="Next" CommandName="Page"
Visible="<%# ((GridView)Container.NamingContainer).PageIndex !=
((GridView)Container.NamingContainer).PageCount - 1 %>">下一页
</asp:LinkButton>
<asp:LinkButton ID="LBtn_LastPage" runat="server" CommandArgument="Last" CommandName="Page"
Visible="<%# ((GridView)Container.NamingContainer).PageIndex !=
((GridView)Container.NamingContainer).PageCount - 1 %>">尾页
</asp:LinkButton>
<asp:TextBox ID="txtNewPageIndex" onkeypress="if (event.keyCode < 48 || event.keyCode >57)
event.returnValue = false;"runat="server" Text='<%#
((GridView)Container.Parent.Parent).PageIndex + 1 %>'Width="20px">
</asp:TextBox>
<asp:Button ID="btn1Go" runat="server" CausesValidation="False" OnClick="btn1Go_Click"
CommandName="Page" Text="跳转">
</asp:Button>
</td>
</tr>
</table>
</PagerTemplate>
2、后台
//绑定GridView显示内容
private void GridViewBind1()
{
string strTemp = "SELECT * FROM Scms_bigclass";
DataSet ds1 = Scms.SystemBase.SQLHelper.DataAdapter(strTemp, Scms.SystemBase.SQLHelper.SDACmd.select, "sqlTemp");
GridView1.DataSource = ds1;
GridView1.DataBind();
}
//点击下一页、上一页
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
GridViewBind1();
}
//点击页面跳转到第几页
protected void btn1Go_Click(object sender, EventArgs e)
{
GridViewRow pagerRow = GridView1.BottomPagerRow;
TextBox txtPage = (TextBox)pagerRow.Cells[0].FindControl("txtNewPageIndex");
if (txtPage.Text == "")
{
txtPage.Text = "1";
}
int numPage = 0;
try
{
numPage = Convert.ToInt32(txtPage.Text);
}
catch
{
numPage = 1;
lblPage_01.Text = "输入错误,请输入数字!";
}
if (numPage < 1)
{
numPage = 1;
lblPage_01.Text = "输入的页数过小当前显示第一页!";
}
if (numPage > GridView1.PageCount)
{
numPage = GridView1.PageCount;
lblPage_01.Text = "输入的页数过大当前显示最后一页!";
}
GridView1.PageIndex = numPage - 1;
GridViewBind1();
}