GridView自定义分页

前后代码:

 <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>页&nbsp;&nbsp;
            <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>&nbsp;&nbsp;
            <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;
            }
        }
        }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值