[研究]ASP.NET,共用GridView1_PageIndexChanging (三)
2024-04-02
環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 19
********************************************************************************
續這篇,再處理 Button_ShowPage_Click 和 Button_ShowAll_Click
[研究]ASP.NET,共用GridView1_PageIndexChanging (二)
https://shaurong.blogspot.com/2024/04/aspnetgridview1pageindexchanging_3.html
********************************************************************************
Common.cs
using System;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public class Common
{
public delegate void PageIndexChangingEventHandler(object sender, GridViewPageEventArgs e);
public delegate void ShowAllButtonClickEventHandler(object sender, EventArgs e);
public delegate void ShowPageButtonClickEventHandler(object sender, EventArgs e);
public static event PageIndexChangingEventHandler PageIndexChangingEvent;
public static event ShowAllButtonClickEventHandler ShowAllButtonClickEvent;
public static event ShowPageButtonClickEventHandler ShowPageButtonClickEvent;
public static void GridView_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();
}
public static void Button_ShowAll_Click(GridView gridView)
{
gridView.AllowPaging = false;
}
public static void Button_ShowPage_Click(GridView gridView)
{
gridView.AllowPaging = true;
}
}
}
|
********************************************************************************
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %> <!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:Button ID="Button_ShowAll" runat="server" Text="全部顯示" OnClick="Button_ShowAll_Click" /> <asp:Button ID="Button_ShowPage" runat="server" Text="分頁顯示" OnClick="Button_ShowPage_Click" /> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyDBConnectionString %>" SelectCommand="SELECT * FROM [MyTable]"></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">第 <asp:Label ID="lblPageIndex" runat="server" Text="<%#((GridView)Container.Parent.Parent).PageIndex + 1 %>" ForeColor="Blue"></asp:Label> 頁, 共 <asp:Label ID="lblPageCount" runat="server" Text="<%# ((GridView)Container.Parent.Parent).PageCount %>" ForeColor="Blue" ></asp:Label> 頁, <asp:LinkButton ID="btnFirst" runat="server" CausesValidation="False" CommandArgument="First" CommandName="Page" Text="第一頁" CssClass="btn btn-primary btn-xs"></asp:LinkButton> <asp:LinkButton ID="btnPrev" runat="server" CausesValidation="False" CommandArgument="Prev" CommandName="Page" Text="上一頁" CssClass="btn btn-primary btn-xs"></asp:LinkButton> <asp:LinkButton ID="btnNext" runat="server" CausesValidation="False" CommandArgument="Next" CommandName="Page" Text="下一頁" CssClass="btn btn-primary btn-xs"></asp:LinkButton> <asp:LinkButton ID="btnLast" runat="server" CausesValidation="False" CommandArgument="Last" CommandName="Page" Text="最後一頁" CssClass="btn btn-primary btn-xs"></asp:LinkButton> 跳到第<asp:TextBox ID="txtNewPageIndex" runat="server" Text="<%# ((GridView)Container.Parent.Parent).PageIndex + 1%>" Width="100px"></asp:TextBox>頁 每頁顯示<asp:TextBox ID="TextBox_PageSize" runat="server" Text="<%# ((GridView)Container.Parent.Parent).PageSize %>" Width="100px"></asp:TextBox>筆 <asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-1" CommandName="Page" Text="GO" CssClass="btn btn-primary btn-xs"></asp:LinkButton> </td> </tr> </table> </PagerTemplate> </asp:GridView> </form> </body> </html> |
Default.aspx.cs
using System; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GridView1.PageIndexChanging += Common.GridView_PageIndexChanging; Common.ShowAllButtonClickEvent += Button_ShowAll_Click; Common.ShowPageButtonClickEvent += Button_ShowPage_Click; } } protected void Button_ShowAll_Click(object sender, EventArgs e) { //Common.Button_ShowAll_Click(GridView1); } protected void Button_ShowPage_Click(object sender, EventArgs e) { //Common.Button_ShowPage_Click(GridView1); } } } |
Default2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default2.aspx.cs" Inherits="WebApplication1.Default2" %> <!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:Button ID="Button_ShowAll" runat="server" Text="全部顯示" OnClick="Button_ShowAll_Click" /> <asp:Button ID="Button_ShowPage" runat="server" Text="分頁顯示" OnClick="Button_ShowPage_Click" /> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyDBConnectionString %>" SelectCommand="SELECT * FROM [MyTable]"></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">第 <asp:Label ID="lblPageIndex" runat="server" Text="<%#((GridView)Container.Parent.Parent).PageIndex + 1 %>" ForeColor="Blue"></asp:Label> 頁, 共 <asp:Label ID="lblPageCount" runat="server" Text="<%# ((GridView)Container.Parent.Parent).PageCount %>" ForeColor="Blue" ></asp:Label> 頁, <asp:LinkButton ID="btnFirst" runat="server" CausesValidation="False" CommandArgument="First" CommandName="Page" Text="第一頁" CssClass="btn btn-primary btn-xs"></asp:LinkButton> <asp:LinkButton ID="btnPrev" runat="server" CausesValidation="False" CommandArgument="Prev" CommandName="Page" Text="上一頁" CssClass="btn btn-primary btn-xs"></asp:LinkButton> <asp:LinkButton ID="btnNext" runat="server" CausesValidation="False" CommandArgument="Next" CommandName="Page" Text="下一頁" CssClass="btn btn-primary btn-xs"></asp:LinkButton> <asp:LinkButton ID="btnLast" runat="server" CausesValidation="False" CommandArgument="Last" CommandName="Page" Text="最後一頁" CssClass="btn btn-primary btn-xs"></asp:LinkButton> 跳到第<asp:TextBox ID="txtNewPageIndex" runat="server" Text="<%# ((GridView)Container.Parent.Parent).PageIndex + 1%>" Width="100px"></asp:TextBox>頁 每頁顯示<asp:TextBox ID="TextBox_PageSize" runat="server" Text="<%# ((GridView)Container.Parent.Parent).PageSize %>" Width="100px"></asp:TextBox>筆 <asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-1" CommandName="Page" Text="GO" CssClass="btn btn-primary btn-xs"></asp:LinkButton> </td> </tr> </table> </PagerTemplate> </asp:GridView> </form> </body> </html> |
Default.aspx2.cs
using System; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GridView1.PageIndexChanging += Common.GridView_PageIndexChanging; Common.ShowAllButtonClickEvent += Button_ShowAll_Click; Common.ShowPageButtonClickEvent += Button_ShowPage_Click; } } protected void Button_ShowAll_Click(object sender, EventArgs e) { //Common.Button_ShowAll_Click(GridView1); } protected void Button_ShowPage_Click(object sender, EventArgs e) { //Common.Button_ShowPage_Click(GridView1); } } } |
(完)
相關
[研究]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
沒有留言:
張貼留言