Page.FindControl Method (String)
.NET Framework 2.0
Searches the page naming container for a server control with the specified identifier.
Namespace: System.Web.UI
Assembly: System.Web (in system.web.dll)
Assembly: System.Web (in system.web.dll)
'Declaration Public Overrides Function FindControl ( _ id As String _ ) As Control 'Usage Dim instance As Page Dim id As String Dim returnValue As Control returnValue = instance.FindControl(id)
public Control FindControl ( String id )
public override function FindControl ( id : String ) : Control
Not applicable.
Parameters
- id
The identifier for the control to be found.
Return Value
The specified control, or a null reference (Nothing in Visual Basic) if the specified control does not exist.The FindControl method can be used to access a control whose ID is not available at design time. The method searches only the page's immediate, or top-level, container; it does not recursively search for controls in naming containers contained on the page. To access controls in a subordinate naming container, call the FindControl method of that container.
The following code example demonstrates how to use the FindControl method to locate controls inside templates. In this example, two Repeater controls are defined; each shows a different way to catch the Click event of a LinkButton inside the repeater item template.
<%@ 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("foo1")) al.Add(New RepeaterObject("foo2")) al.Add(New RepeaterObject("foo3")) 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("foo"), 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("foo"), 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="foo" 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="foo" Text='<%#Eval("RepeaterLabel")%>' /> <asp:LinkButton ID="LinkButton2" Text="Change" runat="server" /> <br /> </ItemTemplate> </asp:Repeater> </asp:Panel> </div> </form> </body> </html>
Community Additions
ADD
Show: