前台代码:
<asp:GridView ID="GridView2" runat="server" AllowPaging="True"
AllowSorting="True" onpageindexchanging="GridView1_PageIndexChanging"
onrowdatabound="GridView1_RowDataBound" PageSize="3" ShowFooter="True"
CellPadding="4" ForeColor="#333333" GridLines="None"
>
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerSettings Visible="False" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<PagerTemplate>
<asp:Button ID="Button1" runat="server" CommandArgument="first"
CommandName="page" Text="|<" Width="30px" />
<asp:Button ID="Button2" runat="server" CommandArgument="prev"
CommandName="page" Text="<" Width="30px" />
<asp:Button ID="Button3" runat="server" CommandArgument="next"
CommandName="page" Text=">" Width="30px" />
<asp:Button ID="Button4" runat="server" CommandArgument="last"
CommandName="page" Text=">|" Width="30px" />
<asp:Label ID="Label1" runat="server"></asp:Label>
</PagerTemplate>
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
<br />
<asp:Button ID="btnFirst" runat="server" οnclick="btnFirst_Click" Text="第一页" />
<asp:Button ID="btnPrev" runat="server" οnclick="btnPrev_Click" Text="上一页" />
<asp:Button ID="btnNext" runat="server" οnclick="btnNext_Click" Text="下一页" />
<asp:Button ID="btnLast" runat="server" οnclick="btnLast_Click" Text="最后一页" />
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindStudent1();
}
}
private void BindStudent1()
{
string sql = "select * from student";
DataTable dt = SqlHelp.ExecuteDataTable(sql);
GridView2.DataSource = dt;
GridView2.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
if (e.NewPageIndex >= 0)
{
GridView1.PageIndex = e.NewPageIndex;
this.BindStudent();
}
}
protected void btnFirst_Click(object sender, EventArgs e)
{
this.GridView2.PageIndex = 0;
this.BindStudent1();
}
protected void btnPrev_Click(object sender, EventArgs e)
{
int index = GridView2.PageIndex;
if (index >= 1)
{
index--;
}
GridView2.PageIndex = index;
this.BindStudent1();
}
protected void btnNext_Click(object sender, EventArgs e)
{
int index = GridView2.PageIndex;
if (index<this.GridView2.PageCount+1)
{
index++;
}
GridView2.PageIndex = index;
this.BindStudent1();
}
protected void btnLast_Click(object sender, EventArgs e)
{
this.GridView2.PageIndex = this.GridView2.PageCount - 1;
this.BindStudent1();
}