2024年4月14日 星期日

[研究]ASP.NET, WebForm, Web User Control : GridView 試用

[研究]ASP.NET, WebForm, Web User Control : GridView 試用

2024-04-12

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 19

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

GridViewUserControl1.acsx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="GridViewUserControl1.ascx.cs" Inherits="WebApplication1.GridViewUserControl1" %>
<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">
    <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>




GridViewUserControl1.acsx.cs

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

namespace WebApplication1
{
    public partial class GridViewUserControl1 : System.Web.UI.UserControl
    {
        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();
        }
    }
}


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

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

<%@ Register TagPrefix="uc" TagName="GridViewControl" Src="GridViewUserControl1.ascx" %>

<!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">
        <uc:GridViewControl ID="GridViewControl1" runat="server" />

    </form>
</body>
</html>



Default.aspx.cs

不用改



********************************************************************************
執行結果

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

注意:警告 在 'GridViewControl' 元素的開頭和結尾標記之間,不允許使用內容。在 Default.aspx 中 <uc:GridViewControl> 中不能用 <columns> 

(完)

相關

沒有留言:

張貼留言