GridView分页用法

本人通过学习GridView,查看的相关资料,总结了实现上一页、下一页、首页、尾页、页面跳转的方法如下

新建一个.aspx页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sjy_Form.aspx.cs" Inherits="Sjy_Form" %>
<%@ OutputCache Duration="2" VaryByParam="*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>数据源管理窗口</title>
    <script language="javascript" src="Js/calendar.js" charset="gb2312"></script>
        <style>
            .c{
                   FILTER: alpha(style=1,opacity=0,finishOpacity=1,startX=1 , startY=1 , finishX=99, finishY=99 );
            }
            html{  
              html { overflow-x:hidden; }  
            }  
        </style>
</head>
<body>
    <form id="form1" runat="server">

          <div id="b" style="left: 11px; overflow-x :auto; overflow-y:auto;
            width: 645px; position: absolute; top: 419px; height: 195px">
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                CellPadding="3" Height="1px" Width="1005px" PageSize="5" Font-Size="10pt" BackColor="White" BorderColor="#E7E7FF" BorderWidth="1px" OnRowDataBound="GridView1_RowDataBound"> 
                <Columns>
                    <asp:BoundField DataField="sjy_id" HeaderText="数据源编号" SortExpression="sjy_id" >
                        <HeaderStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="sjymc" HeaderText="数据源名称" SortExpression="sjymc" >
                        <HeaderStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="sjybt" HeaderText="数据源标题" SortExpression="sjybt" >
                        <HeaderStyle HorizontalAlign="Left" />
                        <ItemStyle Wrap="False" />
                    </asp:BoundField>
                    <asp:BoundField DataField="jlr" HeaderText="建立人">
                        <HeaderStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="jlrq" HeaderText="建立日期" DataFormatString="{0:d}">
                        <HeaderStyle HorizontalAlign="Left" />
                        <ItemStyle Wrap="False" />
                    </asp:BoundField>
                </Columns>
                <PagerTemplate>
                    <table width="100%">
                        <tr>
                            <td style="text-align: right">
                                第<asp:Label ID="lblPageIndex" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1  %>'></asp:Label>页
                                共/<asp:Label ID="lblPageCount" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageCount  %>'></asp:Label>页
                                <asp:LinkButton ID="btnFirst" runat="server" CausesValidation="False" CommandArgument="First"
                                    CommandName="Page" Text="首页" OnClick="LinkBtn_Sjy_Click"></asp:LinkButton>
                                <asp:LinkButton ID="btnPrev" runat="server" CausesValidation="False" CommandArgument="Prev"
                                    CommandName="Page" Text="上一页" OnClick="LinkBtn_Sjy_Click"></asp:LinkButton>
                                <asp:LinkButton ID="btnNext" runat="server" CausesValidation="False" CommandArgument="Next"
                                    CommandName="Page" Text="下一页" OnClick="LinkBtn_Sjy_Click"></asp:LinkButton>
                                <asp:LinkButton ID="btnLast" runat="server" CausesValidation="False" CommandArgument="Last"
                                    CommandName="Page" Text="尾页" OnClick="LinkBtn_Sjy_Click"></asp:LinkButton>
                                <asp:TextBox ID="txtNewPageIndex" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1  %>'
                                    Width="20px"></asp:TextBox>
                                <asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="Go"
                                    CommandName="Page" Text="GO" OnClick="LinkBtn_Sjy_Click"></asp:LinkButton><!-- here set the CommandArgument of the Go Button to '-1' as the flag -->
                            </td>
                        </tr>
                    </table>
                </PagerTemplate>
                <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
                <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                <HeaderStyle BackColor="#7481BA" Font-Bold="True" ForeColor="Yellow" HorizontalAlign="Left" Wrap="False" />
                <AlternatingRowStyle BackColor="#F7F7F7" />
            </asp:GridView>
            <asp:Label ID="Lab_info" runat="server" ForeColor="red"></asp:Label>
        </div>
    </form>
</body>
</html>

 

后台代码:

 

#region Gridview数据填充
    /// <summary>
    /// Gridview数据填充
    /// </summary>
    private void Gridview_Bind()
    {
        sqlStr = "select * from sys_pcedm_sjy";
        DataSet ds = OracleDB.Dataset(sqlStr);
        GridView1.DataSource = ds;
        GridView1.DataBind();

        if (ds.Tables[0].Rows.Count == 0)
        {
            GridView1.Visible = false;
            Lab_info.Text = "当前数据记录为空";
        }
        else
        {
            GridViewRow pagerRow = GridView1.BottomPagerRow;
            LinkButton linkBtnFirst = (LinkButton)pagerRow.Cells[0].FindControl("btnFirst");
            LinkButton linkBtnPrev = (LinkButton)pagerRow.Cells[0].FindControl("btnPrev");
            LinkButton linkBtnNext = (LinkButton)pagerRow.Cells[0].FindControl("btnNext");
            LinkButton linkBtnLast = (LinkButton)pagerRow.Cells[0].FindControl("btnLast");
            if (this.GridView1.PageIndex == 0)
            {
                linkBtnFirst.Enabled = false;
                linkBtnPrev.Enabled = false;
            }
            else
            {
                linkBtnFirst.Enabled = true;
                linkBtnPrev.Enabled = true;
            }

            if (GridView1.PageIndex == GridView1.PageCount - 1)
            {
                linkBtnLast.Enabled = false;
                linkBtnNext.Enabled = false;
            }
            else
            {
                linkBtnLast.Enabled = true;
                linkBtnNext.Enabled = true;
            }

            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
            {
                DataRowView mydrv;
                string gIntro;
                if (GridView1.PageIndex == 0)
                {
                    mydrv = ds.Tables["data"].DefaultView[i];
                    gIntro = Convert.ToString(mydrv["sjysm"]);     //这里取出单元格数据,
                    GridView1.Rows[i].Cells[7].Text = SubStr(gIntro, 4);//自定义了一个函数SubStr进行截取,返回的值重新绑定到gridview控件的单元格中.
                }
                else
                {
                    mydrv = ds.Tables["data"].DefaultView[i + (5 * GridView1.PageIndex)];
                    gIntro = Convert.ToString(mydrv["sjysm"]);
                    GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 4);
                }
            }
        }
    }
    #endregion

 

如果大家有其它好的方法,希望能提出。

共同交流 共同学习

作者:ZHF     
          出处:http://zhf.cnblogs.com/     
          本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 

你可能感兴趣的:(GridView分页用法)