博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Gridview分页(一)
阅读量:5249 次
发布时间:2019-06-14

本文共 5425 字,大约阅读时间需要 18 分钟。

静态页面

<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"
                                οnclick="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
        }
    }

 

}

转载于:https://www.cnblogs.com/weihengblogs/archive/2012/12/04/2801532.html

你可能感兴趣的文章
bzoj 3991: [SDOI2015]寻宝游戏
查看>>
Linux学习-1进程
查看>>
理解Javascript的闭包
查看>>
设计模式之简单工厂模式
查看>>
集成学习算法
查看>>
构造方法和final,static关键字
查看>>
cmd 下切换目录
查看>>
从程序员到项目经理(29):怎样写文档
查看>>
(原创)如何将Nios II硬件和软件合成一个文件(NIOS II)(硬件)(软件)(合并)...
查看>>
浅谈java volatile
查看>>
把linux可执行程序做成一个服务[转]
查看>>
docker入门基础(八)
查看>>
1.tomcat部署项目的几种方式和weblogic部署方式及一点通讯
查看>>
c语言在windows下和Mac下的不同表现!
查看>>
单链表实现的栈
查看>>
AC自动机
查看>>
用C#实现网络爬虫(二)
查看>>
Android 动画之ScaleAnimation应用详解
查看>>
【转】WEKA 中用于学习方案的通用选项
查看>>
java IO和NIO区别
查看>>