前台代码:
我的GridView选择AllowPaging = true(允许分页),但要把<PagerSettings Visible="False" />
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
Default4.aspx.cs
"
Inherits
=
"
Default4
"
%>
<! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< html xmlns = " http://www.w3.org/1999/xhtml " >
< head runat = " server " >
< title > 无标题页 </ title >
</ head >
< body >
< form id = " form1 " runat = " server " >
< div align = " left " >
< asp:GridView ID = " GridView1 " runat = " server " Height = " 134px " Width = " 835px "
CellPadding = " 4 " ForeColor = " #333333 " GridLines = " None " AllowPaging = " True "
AllowSorting = " True " AutoGenerateColumns = " False " PageSize = " 2 " >
< PagerSettings Visible = " False " />
< FooterStyle BackColor = " #507CD1 " Font - Bold = " True " ForeColor = " White " />
< RowStyle BackColor = " #EFF3FB " />
< Columns >
< asp:BoundField DataField = " id " HeaderText = " 学号 " />
< asp:BoundField DataField = " name " HeaderText = " 姓名 " />
< asp:BoundField DataField = " sex " HeaderText = " 性别 " />
< asp:BoundField DataField = " age " HeaderText = " 年龄 " />
< asp:BoundField DataField = " department " HeaderText = " 专业 " />
< asp:BoundField DataField = " grade " HeaderText = " 年级 " />
</ Columns >
< PagerStyle BackColor = " #2461BF " ForeColor = " White " HorizontalAlign = " Center " />
< SelectedRowStyle BackColor = " #D1DDF1 " Font - Bold = " True " ForeColor = " #333333 " />
< HeaderStyle BackColor = " #507CD1 " Font - Bold = " True " ForeColor = " White " />
< EditRowStyle BackColor = " #2461BF " />
< AlternatingRowStyle BackColor = " White " />
</ asp:GridView >
< asp:LinkButton ID = " lnkbtnFirst " runat = " server " onclick = " PagerButtonClick " > 首页 </ asp:LinkButton >
< asp:LinkButton ID = " lnkbtnPre " runat = " server " onclick = " PagerButtonClick " > 上一页 </ asp:LinkButton >
< asp:LinkButton ID = " lnkbtnNext " runat = " server " onclick = " PagerButtonClick " > 下一页 </ asp:LinkButton >
< asp:LinkButton ID = " lnkbtnLast " runat = " server " onclick = " PagerButtonClick " > 尾页 </ asp:LinkButton >
< asp:Label ID = " lblCurrentIndex " runat = " server " Text = " 第?页 " ></ asp:Label >
< asp:Label ID = " lblPageCount " runat = " server " Text = " 共?页 " ></ asp:Label >
< asp:Label ID = " lblRecordCount " runat = " server " Text = " 记录数 " ></ asp:Label >
< asp:TextBox ID = " txtJumpPage " runat = " server " Width = " 24px " > 1 </ asp:TextBox >
< asp:LinkButton ID = " lnkbtnJumpPage " runat = " server "
onclick = " lnkbtnJumpPage_Click " > 跳转 </ asp:LinkButton >
</ div >
</ form >
</ body >
</ html >
<! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< html xmlns = " http://www.w3.org/1999/xhtml " >
< head runat = " server " >
< title > 无标题页 </ title >
</ head >
< body >
< form id = " form1 " runat = " server " >
< div align = " left " >
< asp:GridView ID = " GridView1 " runat = " server " Height = " 134px " Width = " 835px "
CellPadding = " 4 " ForeColor = " #333333 " GridLines = " None " AllowPaging = " True "
AllowSorting = " True " AutoGenerateColumns = " False " PageSize = " 2 " >
< PagerSettings Visible = " False " />
< FooterStyle BackColor = " #507CD1 " Font - Bold = " True " ForeColor = " White " />
< RowStyle BackColor = " #EFF3FB " />
< Columns >
< asp:BoundField DataField = " id " HeaderText = " 学号 " />
< asp:BoundField DataField = " name " HeaderText = " 姓名 " />
< asp:BoundField DataField = " sex " HeaderText = " 性别 " />
< asp:BoundField DataField = " age " HeaderText = " 年龄 " />
< asp:BoundField DataField = " department " HeaderText = " 专业 " />
< asp:BoundField DataField = " grade " HeaderText = " 年级 " />
</ Columns >
< PagerStyle BackColor = " #2461BF " ForeColor = " White " HorizontalAlign = " Center " />
< SelectedRowStyle BackColor = " #D1DDF1 " Font - Bold = " True " ForeColor = " #333333 " />
< HeaderStyle BackColor = " #507CD1 " Font - Bold = " True " ForeColor = " White " />
< EditRowStyle BackColor = " #2461BF " />
< AlternatingRowStyle BackColor = " White " />
</ asp:GridView >
< asp:LinkButton ID = " lnkbtnFirst " runat = " server " onclick = " PagerButtonClick " > 首页 </ asp:LinkButton >
< asp:LinkButton ID = " lnkbtnPre " runat = " server " onclick = " PagerButtonClick " > 上一页 </ asp:LinkButton >
< asp:LinkButton ID = " lnkbtnNext " runat = " server " onclick = " PagerButtonClick " > 下一页 </ asp:LinkButton >
< asp:LinkButton ID = " lnkbtnLast " runat = " server " onclick = " PagerButtonClick " > 尾页 </ asp:LinkButton >
< asp:Label ID = " lblCurrentIndex " runat = " server " Text = " 第?页 " ></ asp:Label >
< asp:Label ID = " lblPageCount " runat = " server " Text = " 共?页 " ></ asp:Label >
< asp:Label ID = " lblRecordCount " runat = " server " Text = " 记录数 " ></ asp:Label >
< asp:TextBox ID = " txtJumpPage " runat = " server " Width = " 24px " > 1 </ asp:TextBox >
< asp:LinkButton ID = " lnkbtnJumpPage " runat = " server "
onclick = " lnkbtnJumpPage_Click " > 跳转 </ asp:LinkButton >
</ div >
</ form >
</ body >
</ html >
后台代码:
using
System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
private void Bind()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
SqlDataAdapter adp = new SqlDataAdapter("select * from information", conn);
DataSet dataset = new DataSet();
try
{
conn.Open();
adp.Fill(dataset, "information");
GridView1.DataSource = dataset;
GridView1.DataKeyNames = new String[] { "id" };
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
finally
{
// 释放占有资源
conn.Close();
}
lblCurrentIndex.Text = "第" + (GridView1.PageIndex + 1).ToString() + "页";
lblPageCount.Text = "总共" + GridView1.PageCount.ToString() + "页";
lblRecordCount.Text = "总共" + dataset.Tables[0].Rows.Count.ToString() + "条";
if (dataset.Tables[0].Rows.Count == 0)
{
lnkbtnFirst.Visible = false;
lnkbtnPre.Visible = false;
lnkbtnNext.Visible = false;
lnkbtnLast.Visible = false;
lblCurrentIndex.Visible = false;
lblPageCount.Visible = false;
lblRecordCount.Visible = false;
}
else if (GridView1.PageCount == 1)
{
lnkbtnFirst.Visible = false;
lnkbtnPre.Visible = false;
lnkbtnNext.Visible = false;
lnkbtnLast.Visible = false;
}
lnkbtnFirst.CommandArgument = "1";
lnkbtnPre.CommandArgument = (GridView1.PageIndex == 0 ? "1" : GridView1.PageIndex.ToString());
lnkbtnNext.CommandArgument = (GridView1.PageCount == 1 ? GridView1.PageCount.ToString() : (GridView1.PageIndex + 2).ToString());
lnkbtnLast.CommandArgument = GridView1.PageCount.ToString();
}
protected void lnkbtnJumpPage_Click(object sender, EventArgs e)
{
GridView1.PageIndex = Convert.ToInt32(txtJumpPage.Text) - 1;
lblCurrentIndex.Text = "第" + (GridView1.PageIndex + 1).ToString() + "页";
Bind();
}
protected void PagerButtonClick(object sender, EventArgs e)
{
GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandArgument) - 1;
Bind();
}
}
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
private void Bind()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
SqlDataAdapter adp = new SqlDataAdapter("select * from information", conn);
DataSet dataset = new DataSet();
try
{
conn.Open();
adp.Fill(dataset, "information");
GridView1.DataSource = dataset;
GridView1.DataKeyNames = new String[] { "id" };
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
finally
{
// 释放占有资源
conn.Close();
}
lblCurrentIndex.Text = "第" + (GridView1.PageIndex + 1).ToString() + "页";
lblPageCount.Text = "总共" + GridView1.PageCount.ToString() + "页";
lblRecordCount.Text = "总共" + dataset.Tables[0].Rows.Count.ToString() + "条";
if (dataset.Tables[0].Rows.Count == 0)
{
lnkbtnFirst.Visible = false;
lnkbtnPre.Visible = false;
lnkbtnNext.Visible = false;
lnkbtnLast.Visible = false;
lblCurrentIndex.Visible = false;
lblPageCount.Visible = false;
lblRecordCount.Visible = false;
}
else if (GridView1.PageCount == 1)
{
lnkbtnFirst.Visible = false;
lnkbtnPre.Visible = false;
lnkbtnNext.Visible = false;
lnkbtnLast.Visible = false;
}
lnkbtnFirst.CommandArgument = "1";
lnkbtnPre.CommandArgument = (GridView1.PageIndex == 0 ? "1" : GridView1.PageIndex.ToString());
lnkbtnNext.CommandArgument = (GridView1.PageCount == 1 ? GridView1.PageCount.ToString() : (GridView1.PageIndex + 2).ToString());
lnkbtnLast.CommandArgument = GridView1.PageCount.ToString();
}
protected void lnkbtnJumpPage_Click(object sender, EventArgs e)
{
GridView1.PageIndex = Convert.ToInt32(txtJumpPage.Text) - 1;
lblCurrentIndex.Text = "第" + (GridView1.PageIndex + 1).ToString() + "页";
Bind();
}
protected void PagerButtonClick(object sender, EventArgs e)
{
GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandArgument) - 1;
Bind();
}
}