ASP.NET如何取得GridView的PagerTemplate內的控制項?

ASP.NET如何取得GridView的PagerTemplate內的控制項?

小弟最近在討論區看到此問題,分享一下我的方法呀..asp.net(c#)

PagerTemplate.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PagerTemplate.aspx.cs" Inherits="PagerTemplate" %>

<!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>PagerTemplate</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="取得PagerTemplate裡的Control" Width="200px" OnClick="Button1_Click" /><br />
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="5" CellPadding="4" ForeColor="#333333" GridLines="None" Width="300px">
                <PagerTemplate>
                    <asp:Label ID="Label1" runat="server" Text="分頁"></asp:Label>
                </PagerTemplate>
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#999999" />
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            </asp:GridView>
        </div>
    </form>
</body>
</html>

PagerTemplate.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public partial class PagerTemplate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //初始Gridview資料
            this.GridView1.DataSource = new string[] { "Dotblogs", "F6 Team", "puma","hent","ajax","asp.net" };
            this.GridView1.DataBind();
        }

    }


    //取得PagerTemplate裡的Label控制項
    protected void Button1_Click(object sender, EventArgs e)
    {
        GridViewRow pagerRow = this.GridView1.BottomPagerRow;

        Label lbl = pagerRow.FindControl("Label1") as Label;

        Response.Write(lbl.Text);
    }

}

執行結果:

參考網址:

http://www.programmer-club.com/pc2020v5/forum/ShowSameTitleN.asp?URL=N&board_pc2020=aspdotnet&index=4&id=18698&mode=&type_pc2020=sametitleLevel-2