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>
<!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























//取得PagerTemplate裡的Label控制項




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

Response.Write(lbl.Text);


執行結果:
參考網址: