GirdView中自己实现分页的例子

对于初学ASP.NET 的人来说,GridView的使用是必不可少的,其中,分页功能也是使用的相当频繁,不过,当你看到这里的时候,这个问题对你来说,已经解决了。
下面就是实现分页的详细代码:
    <div>
    &nbsp;GridView分页</div>
    <asp:GridView ID="MyGV" runat="server" AllowPaging="True" PageSize="2"
PagerStyle-Mode="NumericPages"
PagerStyle-HorizontalAlign="Right"
        BorderColor="Black"
BorderWidth="1px"
CellPadding="3"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="#eeeeee"
AutoGenerateColumns="False" Font-Names="Verdana"
        onpageindexchanging="MyGV_PageIndexChanging">
        <Columns>
            <asp:BoundField DataField="bookid" HeaderText="图书编号" />
            <asp:BoundField DataField="bookname" HeaderText="图书名称" />
        </Columns>
        <PagerStyle HorizontalAlign="Right" />
        <HeaderStyle BackColor="#AAAADD" />
    </asp:GridView>
    <br />
    <asp:LinkButton ID="lbtnfirst" runat="server" CommandArgument="0" ForeColor="navy"
Font-Name="verdana" Font-size="8pt"
        onclick="PageButtonOn_Click">首页</asp:LinkButton>
&nbsp;&nbsp;
    <asp:LinkButton ID="lbtnprev" runat="server" CommandArgument="prev" ForeColor="navy"
Font-Name="verdana" Font-size="8pt"
        onclick="PageButtonOn_Click">前页</asp:LinkButton>
&nbsp;&nbsp;&nbsp;
    <asp:LinkButton ID="lbtnnext" runat="server" CommandArgument="next" ForeColor="navy"
Font-Name="verdana" Font-size="8pt"
        onclick="PageButtonOn_Click">后页</asp:LinkButton>
&nbsp;&nbsp;&nbsp;
    <asp:LinkButton ID="lbtnlast" runat="server" CommandArgument="last"  ForeColor="navy"
Font-Name="verdana" Font-size="8pt"
        onclick="PageButtonOn_Click">末页</asp:LinkButton>
        <asp:Checkbox id="chk1" runat="server"
Text="显示内置的页数"
Font-Name="Verdana"
Font-Size="8pt"
AutoPostBack="true"
/>
        <table bgcolor="#eeeeee" cellpadding="6"><tr><td nowrap><font face="Verdana" size="-2">
<asp:Label id="lblCurrentIndex" runat="server" /><br>
<asp:Label id="lblPageCount" runat="server" /><br>
</font></td></tr></table>
上面是一个Gridview控件,然后用四个LinkButton来实现上下翻页的功能。
如果出现的数据达到了分页的标准(为了测试的方便,我在这里设置的是每页显示2条),则就可以通过选中一个复选框来显示Gridview中内置的页数。
下面的是后台代码:
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //判断是否隐藏内置的页数
            if (chk1.Checked)
            {
                MyGV.PagerSettings.Visible= true;
            }
            else
            {
                MyGV.PagerSettings.Visible = false;
            }
            Bind();

        }
        protected void PageButtonOn_Click(object sender, EventArgs e)
        {
            //获得LinkButton的参数值
            string parm = ((LinkButton)sender).CommandArgument;
            switch (parm)
            {
                case "next":
                    if (MyGV.PageIndex < MyGV.PageCount - 1)
                        MyGV.PageIndex++;
                    break;
                case "prev":
                    if (MyGV.PageIndex >0)
                        MyGV.PageIndex--;
                    break;
                case "last":
                    MyGV.PageIndex = MyGV.PageCount - 1;
                    break;
                default:
                    MyGV.PageIndex =Convert.ToInt32( parm);
                    break;
            }
            Bind();
        }

        void Bind()
        {
            MyGV.DataSource = CreateDataSource();
            MyGV.DataBind();
            ShowStats();
        }
        void ShowStats()
        {
            //显示页面信息
            lblCurrentIndex.Text = "当前页数为: " + ((int)MyGV.PageIndex + 1);
            lblPageCount.Text = "总页数是: " + MyGV.PageCount;
        }
        ICollection CreateDataSource()
        {
            /*
                读取数据库的信息,获得DataView
            */
            try
            {
                SqlConnection MyConnection = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=book;Integrated Security=True");//自己测试时要改呀
                SqlCommand MyDataSetCommand = new SqlCommand("select * from books ", MyConnection);
                SqlDataAdapter da = new SqlDataAdapter(MyDataSetCommand);
                DataSet ds = new DataSet();
                da.Fill(ds, "books");
                return ds.Tables["books"].DefaultView;
            }
            catch(Exception e)
            {
                Response.Write(e.Message);
            }
            return null;
        }
        protected void MyGV_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            MyGV.PageIndex = e.NewPageIndex;
            Bind();
        }
总的来说,Gridview这个控件虽然功能也比较全,不过,太过于死板,在实际应用过程中,我们最好还是自己定义比较好。

你可能感兴趣的:(职场,例子,休闲,GridView分页功能)