GridView分页的使用
时间:2011-01-18 来源:孤独一狼
1. 先把gridview(GridViewX)控件拖放到界面上,绑定数据(bind());同时对DropDownList做如下操作:this.ddlCurrentPage.Items.Clear();
for (int i = 1; i <= this.GridViewX.PageCount; i++)
{
this.ddlCurrentPage.Items.Add(i.ToString());
}
1. this.ddlCurrentPage.SelectedIndex = this.GridViewX.PageIndex;
2. 设置gridview的属性AllowPaging="True" PageSize="n"
3. 拖放几个LinkButton按钮,分别设置如下:
<asp:LinkButton ID="lnkbtnFrist" runat="server" OnClick="lnkbtnFrist_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>
4. 设置几个文本按钮的事件处理函数:
protected void lnkbtnPre_Click(object sender, EventArgs e)
{
if (this.GridViewX.PageIndex > 0)
{
this.GridViewX.PageIndex = this.GridViewX.PageIndex - 1;
bind();
}
}
protected void lnkbtnNext_Click(object sender, EventArgs e)
{
if (this.GridViewX.PageIndex < this.GridViewX.PageCount)
{
this.GridViewX.PageIndex = this.GridViewX.PageIndex + 1;
bind();
}
}
protected void lnkbtnLast_Click(object sender, EventArgs e)
{
this.GridViewX.PageIndex = this.GridViewX.PageCount;
bind();
}
5.DropDownList的事件处理函数:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
this.GridViewX.PageIndex = this.ddlCurrentPage.SelectedIndex;
bind();
}