前后代码:
<asp:GridView ID="GridView1" runat="server" Width="789px"
AutoGenerateColumns="False" onrowcommand="GridView1_RowCommand"
CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True"
onpageindexchanging="GridView1_PageIndexChanging" PageSize="5">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:TemplateField HeaderText="编号" >
<HeaderTemplate >
<asp:CheckBox ID="chkAll" runat="server"/>
<asp:Label ID="Label3" runat="server" Text="编号" ></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Id") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserName" HeaderText="用户姓名" />
<asp:TemplateField HeaderText="评测对象">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("ProductName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ReviewContent" HeaderText="商品评论" />
<asp:BoundField DataField="ReviewDateTime" HeaderText="评论时间" />
<asp:BoundField DataField="IPAddress" HeaderText="IP地址" />
<asp:TemplateField HeaderText="状态">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("IsShowd") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:LinkButton ID="linkSelect" runat="server" CommandName="linkSelect"
CommandArgument='<%# Bind("Id") %>'>查看详情</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<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:Panel id="Panel1" runat="server" Width="500px" HorizontalAlign="Left" BackColor="#D6DFF7"
BorderWidth="0px" BorderColor="White" Height="28px">
共<asp:Label ID="labTogether" runat="server" Text="Label"></asp:Label>页
当前<asp:Label ID="labPresent" runat="server" Text="Label"></asp:Label>页
<asp:LinkButton ID="first" runat="server" οnclick="LinkButton_Click" style="text-decoration:none">首页</asp:LinkButton>
<asp:LinkButton ID="prev" runat="server" οnclick="LinkButton_Click" style="text-decoration:none">上一页</asp:LinkButton>
<asp:LinkButton ID="next" runat="server" οnclick="LinkButton_Click" style="text-decoration:none">下一页</asp:LinkButton>
<asp:LinkButton ID="last" runat="server" οnclick="LinkButton_Click" style="text-decoration:none">尾页</asp:LinkButton>
<asp:TextBox ID="txtPage" runat="server" Width="30px"></asp:TextBox>
<asp:Button ID="btnJump" runat="server" Text="GO" CssClass="button"
οnclick="btnJump_Click" />
</asp:Panel>
后台代码:
protected void LinkButton_Click(object sender, EventArgs e)
{
//单击"首页"按钮
if (((LinkButton)sender).ID == "first")
{
this.GridView1.PageIndex = 0;
Bind();
}
//单击"上一页"按钮
else if (((LinkButton)sender).ID == "prev")
{
this.GridView1.PageIndex = this.GridView1.PageIndex - 1;
Bind();
}
//单击"下一页"按钮
else if (((LinkButton)sender).ID == "next")
{
this.GridView1.PageIndex = this.GridView1.PageIndex + 1;
Bind();
}
else if (((LinkButton)sender).ID == "last")
{
this.GridView1.PageIndex = this.GridView1.PageCount - 1;
//绑定数据的方法
Bind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
//绑定数据的方法
Bind();
}
/// <summary>
/// 跳转到指定的页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnJump_Click(object sender, EventArgs e)
{
if (txtPage.Text!=""&&Convert.ToInt32(txtPage.Text)<=this.GridView1.PageCount&&Convert.ToInt32(txtPage.Text)>0)
{
this.GridView1.PageIndex = int.Parse(txtPage.Text) - 1;
//绑定数据的方法
Bind();
}
else
{
Response.Write("<script>alert('请重新输入!')</script>");
}
}
/// <summary>
/// 给GridView绑定数据
/// </summary>
private void Bind()
{
this.GridView1.DataSource = bll.GetProductReviewsInfo();
GridView1.DataKeyNames = new string[] { "Id" };
GridView1.DataBind();
labTogether.Text = this.GridView1.PageCount.ToString();
labPresent.Text = (GridView1.PageIndex + 1).ToString();
//如果当前页是第一页,则禁用"首页"和"上一页",反之则启用
if (this.GridView1.PageIndex==0)
{
first.Enabled = false;
prev.Enabled = false;
}
else
{
first.Enabled = true;
prev.Enabled = true;
}
//如果当前页是最后一页,则禁用"下一页"和"尾页",反之则启用
if (this.GridView1.PageIndex==this.GridView1.PageCount-1)
{
next.Enabled = false;
last.Enabled = false;
}
else
{
next.Enabled = true;
last.Enabled = true;
}
}
}