2024年4月12日 星期五

[研究][ASP.NET]設定 Gridview 分頁樣式(四):第X頁,共X頁,第一頁 上一頁 下一頁 最後一頁 跳到第X頁 每頁顯示X筆 共X筆

[研究][ASP.NET]設定 Gridview 分頁樣式(四):第X頁,共X頁,第一頁 上一頁 下一頁 最後一頁 跳到第X頁 每頁顯示X筆 共X筆

2024-04-12

總筆數 (不是該分頁的筆數),可改用下面

共&nbsp;<asp:Label ID="Label1" runat="server" Text="<%#  ((IEnumerable)((SqlDataSource)((GridView)Container.Parent.Parent).DataSourceObject).Select(DataSourceSelectArguments.Empty)).Cast<object>().Count() %>" ForeColor="Blue" />&nbsp;筆&nbsp;

或簡化如下

共&nbsp;<asp:Label ID="Label1" runat="server" Text="<%#  ((SqlDataSource)((GridView)Container.Parent.Parent).DataSourceObject).Select(DataSourceSelectArguments.Empty).Cast<object>().Count() %>" ForeColor="Blue" />&nbsp;筆&nbsp;

實際測試可用,程式改成下面

Default6.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default6.aspx.cs"
    Inherits="WebApplication1.Default6" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:SqlDataSource ID="SqlDataSource2" runat="server"
            ConnectionString="<%$ ConnectionStrings:MyDBConnectionString %>"
            SelectCommand="SELECT N'全部' AS [Field1] UNION SELECT [Field1] FROM [MyTable]">
        </asp:SqlDataSource>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
            DataSourceID="SqlDataSource2" DataTextField="Field1" DataValueField="Field1">
        </asp:DropDownList>

        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:MyDBConnectionString %>"
            SelectCommand="SELECT * FROM [MyTable] WHERE (Field1=@Field1 OR @Field1=N'全部')"
            >
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="Field1" PropertyName="SelectedValue" />
            </SelectParameters>
        </asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
            AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="SN"
            DataSourceID="SqlDataSource1"
            OnPageIndexChanging="GridView1_PageIndexChanging">
            <Columns>
                <asp:BoundField DataField="SN" HeaderText="SN" InsertVisible="False" ReadOnly="True" SortExpression="SN" />
                <asp:BoundField DataField="Field1" HeaderText="Field1" SortExpression="Field1" />
                <asp:BoundField DataField="Field2" HeaderText="Field2" SortExpression="Field2" />
            </Columns>
            <PagerTemplate>
                <table>
                    <tr>
                        <td style="text-align: right">第&nbsp;<asp:Label ID="lblPageIndex" runat="server" Text="<%#((GridView)Container.Parent.Parent).PageIndex + 1 %>" ForeColor="Blue"></asp:Label>&nbsp;頁,
                        共&nbsp;<asp:Label ID="lblPageCount" runat="server" Text="<%# ((GridView)Container.Parent.Parent).PageCount %>" ForeColor="Blue"></asp:Label>&nbsp;頁,
                        <asp:LinkButton ID="btnFirst" runat="server" CausesValidation="False" CommandArgument="First" CommandName="Page" Text="第一頁" CssClass="btn btn-primary btn-xs"></asp:LinkButton>&nbsp;&nbsp;
                        <asp:LinkButton ID="btnPrev" runat="server" CausesValidation="False" CommandArgument="Prev" CommandName="Page" Text="上一頁" CssClass="btn btn-primary btn-xs"></asp:LinkButton>&nbsp;&nbsp;
                        <asp:LinkButton ID="btnNext" runat="server" CausesValidation="False" CommandArgument="Next" CommandName="Page" Text="下一頁" CssClass="btn btn-primary btn-xs"></asp:LinkButton>&nbsp;&nbsp;
                        <asp:LinkButton ID="btnLast" runat="server" CausesValidation="False" CommandArgument="Last" CommandName="Page" Text="最後一頁" CssClass="btn btn-primary btn-xs"></asp:LinkButton>&nbsp;&nbsp;
                        跳到第<asp:TextBox ID="txtNewPageIndex" runat="server" Text="<%# ((GridView)Container.Parent.Parent).PageIndex + 1%>" Width="100px"></asp:TextBox>頁&nbsp;
                        每頁顯示<asp:TextBox ID="TextBox_PageSize" runat="server" Text="<%# ((GridView)Container.Parent.Parent).PageSize %>" Width="100px"></asp:TextBox>筆&nbsp;
                        共&nbsp;<asp:Label ID="Label1" runat="server" Text="<%#  ((SqlDataSource)((GridView)Container.Parent.Parent).DataSourceObject).Select(DataSourceSelectArguments.Empty).Cast<object>().Count() %>" ForeColor="Blue" />&nbsp;筆&nbsp;
                        <asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-1" CommandName="Page" Text="GO" CssClass="btn btn-primary btn-xs"></asp:LinkButton>&nbsp;
                        </td>
                    </tr>
                </table>
            </PagerTemplate>
        </asp:GridView>
    </form>
</body>
</html>



Default6.aspx.cs

using System;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class Default6 : System.Web.UI.Page
    {
        // protected int totalRowCount = 0; (用不上了)
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView gvw = (GridView)sender;
            TextBox TextBox_PageSize = (TextBox)gvw.BottomPagerRow.FindControl("TextBox_PageSize");
            bool success = Int32.TryParse(TextBox_PageSize.Text, out int number);
            if (success)
            {
                gvw.PageSize = number;
            }

            if (e.NewPageIndex < 0)
            {
                TextBox pageNum = (TextBox)gvw.BottomPagerRow.FindControl("txtNewPageIndex");
                int Pa = int.Parse(pageNum.Text);
                if (Pa <= 0)
                {
                    gvw.PageIndex = 0;
                }
                else
                {
                    gvw.PageIndex = Pa - 1;
                }
            }
            else
            {
                gvw.PageIndex = e.NewPageIndex;
            }
            //Bind();
        }

        //(用不上了)
        //protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        //{
        //    totalRowCount = e.AffectedRows;
        //}
    }
}




其中 Bootstrap 按鈕樣式可看


********************************************************************************

如果網頁另有「分頁顯示」、「全部顯示」按鈕。.cs要增加Code。

WebForm1.aspx


<asp:Button ID="Button_ShowAll" runat="server" Text="全部顯示" OnClick="Button_ShowAll_Click" CssClass="btn btn-primary btn-xs" />
<asp:Button ID="Button_ShowPage" runat="server" Text="分頁顯示" OnClick="Button_ShowPage_Click" CssClass="btn btn-primary btn-xs" />

WebForm1.aspx.cs

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    //Button_Query_Click(sender, e);
}
protected void Button_ShowAll_Click(object sender, EventArgs e)
{
    GridView1.AllowPaging = false;
    //Button1_Query_Click(sender, e);
}
protected void Button_ShowPage_Click(object sender, EventArgs e)
{
    GridView1.AllowPaging = true;
    //Button1_Query_Click(sender, e);
}

(完)

相關

[研究][ASP.NET]設定 Gridview 分頁樣式(四):第X頁,共X頁,第一頁 上一頁 下一頁 最後一頁 跳到第X頁 每頁顯示X筆 共X筆
https://shaurong.blogspot.com/2024/04/aspnet-gridview-xx-x-x-x_12.html

[研究][ASP.NET]設定 Gridview 分頁樣式(三):第X頁,共X頁,第一頁 上一頁 下一頁 最後一頁 跳到第X頁 每頁顯示X筆 共X筆
https://shaurong.blogspot.com/2024/04/aspnet-gridview-xx-x-x-x.html

[研究]ASP.NET,共用GridView1_PageIndexChanging (三)
https://shaurong.blogspot.com/2024/04/aspnetgridview1pageindexchanging_23.html

[研究]ASP.NET,共用GridView1_PageIndexChanging (二)
https://shaurong.blogspot.com/2024/04/aspnetgridview1pageindexchanging_3.html

[研究]ASP.NET,共用GridView1_PageIndexChanging (一)
https://shaurong.blogspot.com/2024/04/aspnetgridview1pageindexchanging.html

[研究][ASP.NET]設定 Gridview 分頁樣式(二):第X頁,共X頁,第一頁 上一頁 下一頁 最後一頁 跳到第X頁 每頁顯示X筆
https://shaurong.blogspot.com/2020/04/aspnet-gridview-xx-x-x.html

[研究][ASP.NET]設定 Gridview 分頁樣式(一):第X頁 共X頁 第一頁 上一頁 下一頁 最後一頁 跳頁
http://shaurong.blogspot.com/2019/10/aspnet-gridview-x-x.html

Extending GridView Control: Using a DropDownList Pager - CodeProject
這個可以在 PagerTemplate 顯示總資料筆數,但作法很複雜

Aspdotnet-Sharepoint: Custom pager inside the gridview pager template using c#

[ASP.NET] 自訂 GridView PagerTemplate 分頁 | .Net 蛤什麼? - 點部落


沒有留言:

張貼留言