Page.FindControl (Método) (String)
Ensamblado: System.Web (en system.web.dll)
El método FindControl se puede utilizar para tener acceso a un control cuya ID no está disponible en tiempo de diseño. El método realiza la búsqueda únicamente en el contenedor inmediato o de nivel superior de la página; no busca controles de forma recursiva en los contenedores de nombres incluidos en la página. Para tener acceso a los controles de un contenedor de nombres subordinado, llame al método FindControl de dicho contenedor.
En el ejemplo de código siguiente se muestra cómo utilizar el método FindControl para buscar controles dentro de las plantillas. En este ejemplo, se definen dos controles Repeater; cada uno muestra una manera diferente de detectar el evento Click de LinkButton dentro de la plantilla de elementos del repetidor.
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> private class RepeaterObject { private string _string; public RepeaterObject(string label) { _string = label; } public string RepeaterLabel { get { return _string; } set { _string = value; } } } protected void Page_Load() { if (!IsPostBack) { ArrayList al = new ArrayList(); al.Add(new RepeaterObject("foo1")); al.Add(new RepeaterObject("foo2")); al.Add(new RepeaterObject("foo3")); Repeater1.DataSource = al; Repeater2.DataSource = al; DataBind(); } } // This occurs for Repeater1 and originates from LinkButton onClick. protected void OnMyCommand1(object sender, CommandEventArgs e) { LinkButton b = sender as LinkButton; if (b != null) { Label c = (Label)b.Parent.FindControl("foo"); if (c != null) { c.Text = "text changed in handler"; c.ForeColor = System.Drawing.Color.Green; } } } // This occurs for Repeater2 and comes from the Repeater onItemCommand. protected void OnMyCommand2(object sender, RepeaterCommandEventArgs e) { Label l = (Label)e.Item.FindControl("foo"); if (l != null) { l.Text = "text changed in handler"; l.ForeColor = System.Drawing.Color.Red; } } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Page FindControl Example</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Panel ID="Panel1" runat="server" > This repeater sample shows the bubbled event and FindControl when the repeater item OnCommand event occurs.<br /> <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <asp:Label runat="server" ID="foo" Text='<%#Eval("RepeaterLabel")%>' /> <asp:LinkButton Text="Change" runat="server" OnCommand="OnMyCommand1" /> <br /> </ItemTemplate> </asp:Repeater> <hr /> This repeater shows the bubbled event and FindControl when the repeater OnItemCommand event occurs. <br /> <asp:Repeater ID="Repeater2" runat="server" OnItemCommand="OnMyCommand2"> <ItemTemplate> <asp:Label runat="server" ID="foo" Text='<%#Eval("RepeaterLabel")%>' /> <asp:LinkButton Text="Change" runat="server" /> <br /> </ItemTemplate> </asp:Repeater> </asp:Panel> </div> </form> </body> </html>
Windows 98, Windows 2000 Service Pack 4, Windows CE, Windows Millennium, Windows Mobile para Pocket PC, Windows Mobile para Smartphone, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter
Microsoft .NET Framework 3.0 es compatible con Windows Vista, Microsoft Windows XP SP2 y Windows Server 2003 SP1.