GridView控件实现分页
来源:互联网 发布:阿里云自己搭建hadoop 编辑:程序博客网 时间:2024/05/04 19:20
当GridView中显示的记录很多的时候,可以通过GridView的分页功能来分页显示这些记录。
如果GridView是直接绑定数据库,则很简单:只要点击GridView空间左上角的小三角形,再弹出的选项中,将"启动分页"打上勾即可。
如果是用代码实现,则需要这么做:
1、允许分页:设置AllowPaging=True;
2、设置GridView属性栏中PagerSetting里的一些属性中,定义分页的样式;
3、数据部署:将数据显示到GridView上;
4、加入相关事件:PageIndexChanged()、PageIndexChanging();
5、如果要添加分页码显示,即显示当前在第几页,还需添加DataBound()事件。
例子:
功能:GridView分页使用图片按钮并添加分页码显示。
默认情况下GridView的分页按钮如果以图片来显示就无法显示文字,这样就无法知道当前所在的页数。于是,添加分页代码显示就可以显示所在分页的索引数字了。
前台设计:
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="id" HeaderText="行号" />
<asp:BoundField DataField="sid" HeaderText="编号" />
<asp:BoundField DataField="sname" HeaderText="姓名" />
<asp:BoundField DataField="sex" HeaderText="性别" />
<asp:BoundField DataField="age" HeaderText="年龄" />
</Columns>
<PagerSettings Visible="False" />
</asp:GridView>
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="|<" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="<" />
<asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text=">" />
<asp:Button ID="Button4" runat="server" onclick="Button4_Click" Text=">|" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Height="19px" Width="106px"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="CompareValidator"
Operator="DataTypeCheck" Type="Integer"></asp:CompareValidator>
<asp:Button ID="Button5" runat="server" onclick="Button5_Click" Text="go" />
<br />
<br />
<br />
<asp:HiddenField ID="HiddenField1" runat="server" Visible="False" />
<asp:HiddenField ID="HiddenField2" runat="server" Visible="False" />
</div>
</form>
</body>
后台设计:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace _12_6内容.存储分页
{
public partial class 分页 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int pageIndex = 1;//先定义pageIndex
BindStudent(pageIndex);
}
}
private void BindStudent(int pageIndex)
{
string constr = ConfigurationManager.ConnectionStrings["studentConnectionString"].ConnectionString;//在Web.config页设计连接数据库
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;//第一步
//方法1:
//string sp_name = "sp_Student_Select_by_Page_rowNumber";
//cmd.CommandText = sp_name;
//方法2:
cmd.CommandText = "sp_Student_Select_by_Page_rowNumber";//第二步
//第三步:添加参数
cmd.Parameters.AddWithValue("@pageSize", 3);//参数1,已经添加到cmd对象中了
SqlParameter p2 = new SqlParameter("@pageCount", System.Data.SqlDbType.Int);//参数2
p2.Direction = ParameterDirection.Output;//第二步:只有指出第一步才会有第二步,注意命名空间
cmd.Parameters.Add(p2);//添加了参数2
cmd.Parameters.AddWithValue("@pageIndex", pageIndex);//调用pageIndex
SqlDataAdapter adpter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adpter.Fill(dt);
this.GridView1.DataSource = dt;
this.DataBind();
string pageCount = cmd.Parameters["@pageCount"].Value.ToString();
this.HiddenField1.Value = pageCount;
this.HiddenField2.Value = pageIndex.ToString();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
this.BindStudent(1);
}
protected void Button4_Click(object sender, EventArgs e)
{
this.BindStudent(Convert.ToInt32(this.HiddenField1.Value));
}
protected void Button2_Click(object sender, EventArgs e)
{
int index = Convert.ToInt32(this.HiddenField2.Value);
if (index > 1)//这里的pageIndex和GridView中的属性pageIndex不同,属性中的pageIndex是系统设定的,它的起始值是0,表示第一页
//而存储过程中的pageIndex是自己设置的变量,1就表示第一页,所以 if (index > 1)
{
index--;
this.BindStudent(index);
}
}
protected void Button3_Click(object sender, EventArgs e)
{
int index = Convert.ToInt32(this.HiddenField2.Value);
int pageCount=Convert.ToInt32(this.HiddenField1.Value);
if (index < pageCount)
{
index++;
this.BindStudent(index);
}
}
protected void Button5_Click(object sender, EventArgs e)
{
this.BindStudent(Convert.ToInt32(TextBox1.Text));
}
}
}
- GridView控件实现分页
- GridView控件实现分页
- GridView控件实现分页功能
- GridView控件实现分页功能
- GridView控件自定义分页的实现
- 使用AspNetPager控件实现GridView分页
- VS-利用GridView控件实现假分页
- 利用GridView控件实现分页效果
- 如何实现GridView控件分页显示数据
- GridView控件自定义分页
- [GridView控件]自定义分页
- GridView控件自定义分页
- GridView控件的分页
- 自定义GridView 分页控件
- 自定义GridView 分页控件
- Gridview控件的分页
- gridView分页控件
- GridView控件实现支持分页的自动编号代码
- 23种设计模式之单例模式
- 搜索文章收藏
- session
- Linux Platform驱动程序框架解析
- C8825D解锁步骤
- GridView控件实现分页
- 23个C#经典实用技巧
- kewastUnPackStats(): bad magic 1 (0xb72c30ae, 0)
- SQL的基本语法
- Fedora或者Centos常见删除安装命令
- 2012年12月第二个周末
- 如何阅读Java源码 阅读java的真实体会
- 近段时间工作汇总
- Python类的特性方法