asp.net2.0分页的实现(GridView)

一、cs 页面代码

 protected void Page_Load(object sender, System.EventArgs e)
  {
            if (!Page.IsPostBack)
            {
                GridViewBind("");
            }
           
  }

 private void GridViewBind(string Sqlsort)
        {
            try
            {
                //打开数据库连接
                ConnStr = ConfigurationSettings.AppSettings["connstr"];
                SqlConnection con = new SqlConnection(ConnStr);
                con.Open();
                //绑定控件
                SqlDataAdapter cmd = new SqlDataAdapter("select id,title,inputdate from Article where id in (select articleid from Ct_Class where classid like '001005') and (isValidate = 1) order by InputDate desc,px desc", con);
                DataSet ds = new DataSet();
                cmd.Fill(ds, "Article");
                GridView1.DataSource = ds.Tables["Article"].DefaultView;
                GridView1.DataBind();
                LblCurrentIndex.Text = (GridView1.PageIndex + 1).ToString();
                LblPageCount.Text = GridView1.PageCount.ToString();
                LblRecordCount.Text = ds.Tables[0].Rows.Count.ToString();
                if (ds.Tables[0].Rows.Count == 0)
                {
                    btnFirst.Visible = false;
                    btnPrev.Visible = false;
                    btnNext.Visible = false;
                    btnLast.Visible = false;

                    LblCurrentIndex.Visible = false;
                    LblPageCount.Visible = false;
                    LblRecordCount.Visible = false;
                    Label1.Visible = false;
                    Label1.Visible = false;
                    Label2.Visible = false;
                    Label3.Visible = false;
                    Label4.Visible = false;
                    Label5.Visible = false;
                    Label6.Visible = false;
                    lbMessage.Text = "没有记录!";
                }
                else if (GridView1.PageCount == 1)
                {
                    btnFirst.Visible = false;
                    btnPrev.Visible = false;
                    btnNext.Visible = false;
                    btnLast.Visible = false;
                }
                // 计算生成分页页码,分别为:"首 页" "上一页" "下一页" "尾 页"
                btnFirst.CommandName = "1";
                btnPrev.CommandName = (GridView1.PageIndex == 0 ? "1" : GridView1.PageIndex.ToString());
                btnNext.CommandName = (GridView1.PageCount == 1 ? GridView1.PageCount.ToString() : (GridView1.PageIndex + 2).ToString());
                btnLast.CommandName = GridView1.PageCount.ToString();
                //
            }
            catch (Exception ex)
            {
                Response.Write("数据库错误,错误原因:" + ex.Message);
                Response.End();
            }
        }
        protected void PagerButtonClick(object sender, EventArgs e)
        {
            GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
            GridViewBind("");
        }
        protected void GridView1_PageIndexChanging(Object sender, GridViewPageEventArgs e)
        {
            if (GridView1.PageCount > 1)
            {
                GridView1.PageIndex = e.NewPageIndex;
                GridViewBind("");
            }
            else
            {
                LblCurrentIndex.Text = GridView1.EditIndex.ToString();
            }
    }

二、aspx页代码

<center><asp:Label ID="lbMessage" runat="server" Text=""></asp:Label></center><asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging"
                                                    PageSize="15" ShowHeader="False" Width="606px" BorderWidth="0px">
                                                    <PagerSettings FirstPageText="" LastPageText="" Mode="NextPreviousFirstLast"
                                                        NextPageText="" PreviousPageText="" />
                                                    <Columns>
                                                        <asp:TemplateField>
                                                            <ItemTemplate>
                 ·<asp:Label ID="Label1" runat="server" ><A class=zw href='newsshow.aspx?ID=<%# DataBinder.Eval(Container.DataItem,"ID")%>' target=_blank>
                  <%# f1.CutStr(DataBinder.Eval(Container.DataItem,"Title").ToString(),44)%>
                 </A></asp:Label> [<%# f1.getdate(DataBinder.Eval(Container.DataItem,"InputDate").ToString(),10)%>]
                 <hr color=#E1E1E1 size=1 />
                                                            </ItemTemplate>
                                                        </asp:TemplateField>
                                                    </Columns>
                                                    <RowStyle Height="30px" />
                                                    <PagerStyle ForeColor="#FF8000" HorizontalAlign="Center" />
                                                </asp:GridView>
                                                <table border="0" cellpadding="0" cellspacing="0" class="PageTableCss" width="100%">
                                                    <tr>
                                                        <td class="PageLableCss" style="height: 41px">
                                                            <asp:Label ID="Label5" runat="server" Text="总共【"></asp:Label>
                                                            <asp:Label ID="LblRecordCount" runat="server"></asp:Label>
                                                            <asp:Label ID="Label6" runat="server" Text="】条"></asp:Label>
                                                            <asp:Label ID="Label3" runat="server" Text="共【"></asp:Label>
                                                            <asp:Label ID="LblPageCount" runat="server"></asp:Label>
                                                            <asp:Label ID="Label4" runat="server" Text="】页"></asp:Label>
                                                            <asp:Label ID="Label1" runat="server" Text="第【"></asp:Label>
                                                            <asp:Label ID="LblCurrentIndex" runat="server"></asp:Label>
                                                            <asp:Label ID="Label2" runat="server" Text="】页"></asp:Label>
                                                        </td>
                                                        <td class="PageLinkButtonCss" style="height: 41px">
                                                            <asp:LinkButton ID="btnFirst" runat="server" CommandArgument="first" OnClick="PagerButtonClick">首 页</asp:LinkButton>
                                                            <asp:LinkButton ID="btnPrev" runat="server" CommandArgument="prev" OnClick="PagerButtonClick">上一页</asp:LinkButton>
                                                            <asp:LinkButton ID="btnNext" runat="server" CommandArgument="next" OnClick="PagerButtonClick">下一页</asp:LinkButton>
                                                            <asp:LinkButton ID="btnLast" runat="server" CommandArgument="last" OnClick="PagerButtonClick">尾 页</asp:LinkButton>
                                                        </td>
                                                    </tr>
                                                </table>

效果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值