Gridview分页(一)

静态页面

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                            Width="100%" OnPageIndexChanging="GridView1_PageIndexChanging" 
                              OnRowEditing="GridView1_RowEditing"
                            PageSize="12" AllowPaging="True" GridLines="None">
                            <Columns>
      
                                <asp:TemplateField HeaderText="">
                            <ItemTemplate>
                          <br/>
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='<%#"NewShow.aspx?Newsid="+Eval("Newsid")%>' target="_self" title='<%#Eval("NewsTitle")%>'><%#Eval("NewsTitle").ToString().Length > 50 ? Eval("NewsTitle").ToString().Substring(0, 50) : Eval("NewsTitle")%></a>
                        
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Left" Width="500px" />
                                                         
                        </asp:TemplateField>
                            
                                   <asp:TemplateField HeaderText="">
                                <ItemTemplate>
                                    (  <%# Convert.ToDateTime(Eval("CreateDate")).ToString("yyyy-MM-dd-hh:ss")%> )
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" Width="150px" />
                            </asp:TemplateField>
                              
                            </Columns>
                            <PagerSettings Visible="False" />
                            <RowStyle HorizontalAlign="Center" />
                            <HeaderStyle HorizontalAlign="Center" />
                        </asp:GridView>
                       
                         <br/>
                        <td style="text-indent:15px; " class="style1" align=center>
                            <center><asp:LinkButton ID="btnFirst" runat="server" OnClick="PagerButtonClick" Visible="false" Enabled="false"  >首页</asp:LinkButton>
                            <asp:LinkButton ID="btnPrev" runat="server" OnClick="PagerButtonClick" Visible="false" Enabled="false">上一页</asp:LinkButton>
                            <asp:LinkButton ID="btnNext" runat="server" OnClick="PagerButtonClick" Visible="false" Enabled="false">下一页</asp:LinkButton>
                            <asp:LinkButton ID="btnLast" runat="server" OnClick="PagerButtonClick" Visible="false" Enabled="false">尾页</asp:LinkButton>
                                &nbsp;
                       <%--   到:<asp:TextBox id="txtPage" Runat="server" Width="32px"></asp:TextBox>
      页<asp:Button id="btnPage" Runat="server" Width="32px" Text="Go"
                                onclick="btnPage_Click" style="height: 21px"></asp:Button>--%>
                            <asp:Label ID="LblCurrentIndex" runat="server" Visible="False"></asp:Label>
                            <asp:Label ID="LblPageCount" runat="server" Visible="False"></asp:Label>
                            <asp:Label ID="LblPageSize" runat="server" Visible="false"></asp:Label>
                            <asp:Label ID="LblRecordCount" runat="server" Visible="False"></asp:Label></center>
                          &nbsp;
                        </td>

 

后台代码

 

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class News : System.Web.UI.Page
{

    DBHelper db = new DBHelper();
    Hashtable ht = new Hashtable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGirdView();

        }
    }


    public void BindGirdView()
    {
        DataTable dt = db.GetDataTable("select NewsId,NewsTitle,NewsContents,CreateDate  from tblNews order by CreateDate DESC", ht);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        if (dt != null)
        {
            int count = dt.Rows.Count;
            GridViewPagination(count);
        }
        else
        {
            GridViewPagination(0);
        }

    }

    #region 分页控件显示

    /// <summary>
    /// 分页控件显示
    /// </summary>
    /// <param name="RowsCount"></param>
    private void GridViewPagination(int RowsCount)
    {
        LblCurrentIndex.Text = "第 " + (GridView1.PageIndex + 1).ToString() + " 页";
        LblPageCount.Text = "共 " + GridView1.PageCount.ToString() + " 页";
        LblRecordCount.Text = "总共 " + RowsCount.ToString() + " 条";
        LblPageSize.Text = "每页 " + GridView1.PageSize + " 条";
        if (RowsCount == 0)
        {
            SetPagerControlStyle(false, false, false);
        }
        else if (GridView1.PageCount == 1)
        {
            SetPagerControlStyle(true, false, true);
        }
        else
        {
            SetPagerControlStyle(true, true, true);
            if (GridView1.PageIndex == 0)
            {
                btnFirst.Enabled = false;
                btnPrev.Enabled = false;

            }
            else if (GridView1.PageIndex == (GridView1.PageCount - 1))
            {
                btnNext.Enabled = false;
                btnLast.Enabled = 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();
    }

    private void SetPagerControlStyle(bool btnVisible, bool btnEnable, bool lblVisible)
    {
        btnFirst.Visible = btnVisible;
        btnPrev.Visible = btnVisible;
        btnNext.Visible = btnVisible;
        btnLast.Visible = btnVisible;

        btnFirst.Enabled = btnEnable;
        btnPrev.Enabled = btnEnable;
        btnNext.Enabled = btnEnable;
        btnLast.Enabled = btnEnable;

        LblCurrentIndex.Visible = lblVisible;
        LblPageCount.Visible = lblVisible;
        LblRecordCount.Visible = lblVisible;
        LblPageSize.Visible = lblVisible;
    }

    protected void PagerButtonClick(object sender, EventArgs e)
    {
        GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
        BindGirdView();
    }

    #endregion
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindGirdView();
    }
  
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Label olabel;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            olabel = (Label)e.Row.Cells[0].FindControl("lblNo");
            olabel.Text = Convert.ToString(e.Row.RowIndex + 1);
        }
    }
    protected void GridView1_PreRender(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#aabbbb';");
            row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
            //de4ec5
        }
    }

 

}

posted @ 2012-12-04 16:01  盛开的雨季  阅读(179)  评论(0编辑  收藏  举报