Page.FindControl(String) Método
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Busca un control de servidor con el identificador especificado en el contenedor de nomenclatura de la página.
public:
override System::Web::UI::Control ^ FindControl(System::String ^ id);
public override System.Web.UI.Control FindControl (string id);
override this.FindControl : string -> System.Web.UI.Control
Public Overrides Function FindControl (id As String) As Control
Parámetros
- id
- String
Identificador del control que se va a buscar.
Devoluciones
Control especificado, o null
si el control especificado no existe.
Ejemplos
En el ejemplo de código siguiente se muestra cómo usar el FindControl método para buscar controles dentro de plantillas. En este ejemplo, se definen dos Repeater controles; cada uno muestra una manera diferente de detectar el Click evento de un LinkButton elemento dentro de la plantilla de elemento de repetición.
<%@ 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("RepeaterObject1"));
al.Add(new RepeaterObject("RepeaterObject2"));
al.Add(new RepeaterObject("RepeaterObject3"));
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("Label1");
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("Label1");
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="Label1" 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="Label1" Text='<%#Eval("RepeaterLabel")%>' />
<asp:LinkButton Text="Change" runat="server" /> <br />
</ItemTemplate>
</asp:Repeater>
</asp:Panel>
</div>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<!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 As String
Public Sub New(ByVal label As String)
_string = label
End Sub
Public Property RepeaterLabel() As String
Get
Return _string
End Get
Set(ByVal value As String)
_string = value
End Set
End Property
End Class
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If (Not IsPostBack) Then
Dim al As New ArrayList()
al.Add(New RepeaterObject("RepeaterObject1"))
al.Add(New RepeaterObject("RepeaterObject2"))
al.Add(New RepeaterObject("RepeaterObject3"))
Repeater1.DataSource = al
Repeater2.DataSource = al
DataBind()
End If
End Sub
' This occurs for Repeater1 and originates from LinkButton onClick.
Protected Sub OnMyCommand1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
Dim b As LinkButton = sender
If Not (b Is Nothing) Then
Dim c As Label = CType(b.Parent.FindControl("Label1"), Label)
If Not (c Is Nothing) Then
c.Text = "text changed in handler"
c.ForeColor = System.Drawing.Color.Green
End If
End If
End Sub
' This occurs for Repeater2 and comes from the Repeater onItemCommand.
Protected Sub OnMyCommand2(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs)
Dim l As Label = CType(e.Item.FindControl("Label1"), Label)
If Not (l Is Nothing) Then
l.Text = "text changed in handler"
l.ForeColor = System.Drawing.Color.Red
End If
End Sub
</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="Label1" Text='<%#Eval("RepeaterLabel")%>' />
<asp:LinkButton ID="LinkButton1" 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="Label1" Text='<%#Eval("RepeaterLabel")%>' />
<asp:LinkButton ID="LinkButton2" Text="Change" runat="server" /> <br />
</ItemTemplate>
</asp:Repeater>
</asp:Panel>
</div>
</form>
</body>
</html>
Comentarios
El FindControl método se puede usar para acceder a un control cuyo ID no está disponible en tiempo de diseño. El método busca solo el contenedor inmediato o de nivel superior de la página; no busca de forma recursiva controles en contenedores de nomenclatura contenidos en la página. Para acceder a los controles de un contenedor de nomenclatura subordinado, llame al FindControl
método de ese contenedor.