GridView 自定义分页
在html视图中添加
<asp:LinkButton ID="lnkbtnFirst" runat="server" OnClick="lnkbtnFirst_Click">首页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnPre" runat="server" OnClick="lnkbtnPre_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnNext" runat="server" OnClick="lnkbtnNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnLast" runat="server" OnClick="lnkbtnLast_Click">尾页</asp:LinkButton>
在源代码中添加这些事件
注意:BindGridView();是绑定GridView1的方法,这个相信大家都会
/// <summary>
/// 首页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lnkbtnFirst_Click(object sender, EventArgs e)
{
this.GridView1.PageIndex = 0;
BindGridView();
}
/// <summary>
/// 上一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lnkbtnPre_Click(object sender, EventArgs e)
{
if (this. GridView1.PageIndex > 0)
{
this. GridView1.PageIndex = this. GridView1.PageIndex - 1;
BindGridView();
}
}
/// <summary>
/// 下一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lnkbtnNext_Click(object sender, EventArgs e)
{
if (this. GridView1.PageIndex < this. GridView1.PageCount)
{
this. GridView1.PageIndex = this. GridView1.PageIndex+1;
BindGridView();
}
}
/// <summary>
/// 末页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lnkbtnLast_Click(object sender, EventArgs e)
{
this.GridView1.PageIndex = this.GridView1.PageCount;
BindGridView();
}
protected void gvDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
BindGridView();
}
说明:需要设置GridView的AllowPading属性为:True,PageSize:3,PagerSettings下的Visible属性为:false,当然也可以在程序中指定属性.
在html视图中添加
<asp:LinkButton ID="lnkbtnFirst" runat="server" OnClick="lnkbtnFirst_Click">首页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnPre" runat="server" OnClick="lnkbtnPre_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnNext" runat="server" OnClick="lnkbtnNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnLast" runat="server" OnClick="lnkbtnLast_Click">尾页</asp:LinkButton>
在源代码中添加这些事件
注意:BindGridView();是绑定GridView1的方法,这个相信大家都会
/// <summary>
/// 首页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lnkbtnFirst_Click(object sender, EventArgs e)
{
this.GridView1.PageIndex = 0;
BindGridView();
}
/// <summary>
/// 上一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lnkbtnPre_Click(object sender, EventArgs e)
{
if (this. GridView1.PageIndex > 0)
{
this. GridView1.PageIndex = this. GridView1.PageIndex - 1;
BindGridView();
}
}
/// <summary>
/// 下一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lnkbtnNext_Click(object sender, EventArgs e)
{
if (this. GridView1.PageIndex < this. GridView1.PageCount)
{
this. GridView1.PageIndex = this. GridView1.PageIndex+1;
BindGridView();
}
}
/// <summary>
/// 末页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lnkbtnLast_Click(object sender, EventArgs e)
{
this.GridView1.PageIndex = this.GridView1.PageCount;
BindGridView();
}
protected void gvDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
BindGridView();
}