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

 

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

共同交流 共同学习

GridView 是一个常用的控件,用于在网页上展示数据。当数据量较大时,我们通常需要对 GridView 进行分页,以便用户能够方便地浏览数据。下面是 GridView 分页的使用方法: 1..aspx 页面上添加 GridView 控件,并设置 AllowPaging 属性为 True。 ```html <asp:GridView ID="GridView1" runat="server" AllowPaging="True"></asp:GridView> ``` 2..aspx.cs 页面中编写代码,设置 GridView 的数据源并绑定数据。同时,设置 GridView 的 PageSize 属性为每页显示的数据量。最后,调用 DataBind() 方法绑定数据。 ```c# protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // 设置数据源 DataTable dt = GetData(); // 设置每页显示的数据量 GridView1.PageSize = 10; // 绑定数据 GridView1.DataSource = dt; GridView1.DataBind(); } } private DataTable GetData() { // 从数据库中获取数据 // ... // 返回数据表 return dt; } ``` 3..aspx 页面上添加 PagerTemplate,用于显示分页按钮。 ```html <asp:GridView ID="GridView1" runat="server" AllowPaging="True"> <PagerTemplate> <asp:LinkButton ID="lnkPrev" runat="server" CommandName="Page" CommandArgument="Prev">上一页</asp:LinkButton> <% for (int i = 0; i < GridView1.PageCount; i++) { %> <asp:LinkButton ID="lnkPage" runat="server" CommandName="Page" CommandArgument="<%# i %>" Text="<%# i + 1 %>"></asp:LinkButton> <% } %> <asp:LinkButton ID="lnkNext" runat="server" CommandName="Page" CommandArgument="Next">下一页</asp:LinkButton> </PagerTemplate> </asp:GridView> ``` 4..aspx.cs 页面中编写代码,处理分页按钮的事件。 ```c# protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Page") { // 获取当前页码 int pageIndex = GridView1.PageIndex; if (e.CommandArgument.ToString() == "Prev") { // 上一页 if (pageIndex > 0) { pageIndex--; } } else if (e.CommandArgument.ToString() == "Next") { // 下一页 if (pageIndex < GridView1.PageCount - 1) { pageIndex++; } } else { // 其他页码 pageIndex = Convert.ToInt32(e.CommandArgument); } // 设置当前页码 GridView1.PageIndex = pageIndex; // 重新绑定数据 GridView1.DataSource = GetData(); GridView1.DataBind(); } } ``` 通过以上步骤,就可以实现 GridView分页功能了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值