Sideway
output.to from Sideway
Draft for Information Only

Content

Page.FindControl(String) Method
 Definition
 Parameters
 Returns
 Examples
   Examples of ASP.NET C# page
   Examples of ASP.NET VB page
  Remarks
  Applies to
  See also
 Sources and References

Page.FindControl(String) Method

Definition

Namespace: System.Web.UI Assembly: System.Web.dll Searches the page naming container for a server control with the specified identifier.
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

Parameters

id String The identifier for the control to be found.

Returns

Control The specified control, or null if the specified control does not exist.

Examples

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.

Examples of ASP.NET C# page

Examples of ASP.NET C# page
ASP.NET Code Input:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head runat="server">
        <title>Sample Page</title>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
        <%@ Page Language="C#" %>
        <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>
    </head>
    <body>
        <form id="form1" runat="server">
            <h3>Page FindControl Example</h3>
            <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")%>' />&nbsp;
                        <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")%>' />&nbsp;
                        <asp:LinkButton Text="Change" runat="server" /> <br />
                    </ItemTemplate>
                </asp:Repeater>
            </asp:Panel>

            </div>
        </form>
    </body>
</html>
HTTP Response Output:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head><title>
	Sample Page
</title><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /></head>
    <body>
        <form method="post" action="./aspnet_page_findcontrol_001a_01.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTk0MTM5NjM2Mw9kFgICAw9kFgICAQ9kFgQCAQ8WAh4LXyFJdGVtQ291bnQCAxYGZg9kFgICAQ8PFgIeBFRleHQFD1JlcGVhdGVyT2JqZWN0MWRkAgEPZBYCAgEPDxYCHwEFD1JlcGVhdGVyT2JqZWN0MmRkAgIPZBYCAgEPDxYCHwEFD1JlcGVhdGVyT2JqZWN0M2RkAgMPFgIfAAIDFgZmD2QWAgIBDw8WAh8BBQ9SZXBlYXRlck9iamVjdDFkZAIBD2QWAgIBDw8WAh8BBQ9SZXBlYXRlck9iamVjdDJkZAICD2QWAgIBDw8WAh8BBQ9SZXBlYXRlck9iamVjdDNkZGSYsTbU6UsUanv71jyfFbS3rxwEkJyYO73IZGRhMeQSVw==" />
</div>

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>


<div class="aspNetHidden">

	<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="996B5080" />
	<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEdAAc2LtfkbJf0zBjy8C7TNT/xvDbh/DdFbrmr1eeCppHqrA6xp2dORkRvKjgDAUGs3gWJcRY3XbNOnW5mIsqvEsO7JM6Voxj8+yiBDl7bkBwumHupginiIWVqNKZ0csqS6K/KSukbuIffwAZybAHRbQlXi9jL9xlNIc8YDBNaR9xAL9zwSbew0J6j7iWPwsB+ydg=" />
</div>
            <h3>Page FindControl Example</h3>
            <div>

            <div id="Panel1">
	
                This repeater sample shows the bubbled event and FindControl when the repeater item OnCommand event occurs.<br />
                                 <span id="Repeater1_Label1_0">RepeaterObject1</span>&nbsp;
                        <a href="javascript:__doPostBack(&#39;Repeater1$ctl00$ctl00&#39;,&#39;&#39;)">Change</a> <br />
                                     <span id="Repeater1_Label1_1">RepeaterObject2</span>&nbsp;
                        <a href="javascript:__doPostBack(&#39;Repeater1$ctl01$ctl00&#39;,&#39;&#39;)">Change</a> <br />
                                     <span id="Repeater1_Label1_2">RepeaterObject3</span>&nbsp;
                        <a href="javascript:__doPostBack(&#39;Repeater1$ctl02$ctl00&#39;,&#39;&#39;)">Change</a> <br />
                             <hr />

                This repeater shows the bubbled event and FindControl when the repeater OnItemCommand event occurs. <br />
                                 <span id="Repeater2_Label1_0">RepeaterObject1</span>&nbsp;
                        <a href="javascript:__doPostBack(&#39;Repeater2$ctl00$ctl00&#39;,&#39;&#39;)">Change</a> <br />
                                     <span id="Repeater2_Label1_1">RepeaterObject2</span>&nbsp;
                        <a href="javascript:__doPostBack(&#39;Repeater2$ctl01$ctl00&#39;,&#39;&#39;)">Change</a> <br />
                                     <span id="Repeater2_Label1_2">RepeaterObject3</span>&nbsp;
                        <a href="javascript:__doPostBack(&#39;Repeater2$ctl02$ctl00&#39;,&#39;&#39;)">Change</a> <br />
                  </div>

            </div>
        </form>
    </body>
</html>
ASPX Web Page Embedded Output:

Examples of ASP.NET VB page

Examples of ASP.NET VB page
ASP.NET Code Input:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head runat="server">
        <title>Sample Page</title>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
        <%@ Page Language="vb" %>
        <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>
    </head>
    <body>
        <form id="form1" runat="server">
            <h3>Page FindControl Example</h3>
            <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")%>' />&nbsp;
                        <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")%>' />&nbsp;
                        <asp:LinkButton Text="Change" runat="server" /> <br />
                    </ItemTemplate>
                </asp:Repeater>
            </asp:Panel>

            </div>
        </form>
    </body>
</html>
HTTP Response Output:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head><title>
	Sample Page
</title><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /></head>
    <body>
        <form method="post" action="./aspnet_page_findcontrol_001a_02.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTk0MTM5NjM2Mw9kFgICAw9kFgICAQ9kFgQCAQ8WAh4LXyFJdGVtQ291bnQCAxYGZg9kFgICAQ8PFgIeBFRleHQFD1JlcGVhdGVyT2JqZWN0MWRkAgEPZBYCAgEPDxYCHwEFD1JlcGVhdGVyT2JqZWN0MmRkAgIPZBYCAgEPDxYCHwEFD1JlcGVhdGVyT2JqZWN0M2RkAgMPFgIfAAIDFgZmD2QWAgIBDw8WAh8BBQ9SZXBlYXRlck9iamVjdDFkZAIBD2QWAgIBDw8WAh8BBQ9SZXBlYXRlck9iamVjdDJkZAICD2QWAgIBDw8WAh8BBQ9SZXBlYXRlck9iamVjdDNkZGRgmmMDX8lyTAcZskQBf+7fPN9bYbCgldAbBfc5AtJLvg==" />
</div>

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>


<div class="aspNetHidden">

	<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="5A77A5E2" />
	<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEdAAcdi1YFUzZSeYlmH+qe53OYvDbh/DdFbrmr1eeCppHqrA6xp2dORkRvKjgDAUGs3gWJcRY3XbNOnW5mIsqvEsO7JM6Voxj8+yiBDl7bkBwumHupginiIWVqNKZ0csqS6K/KSukbuIffwAZybAHRbQlX4FmPeER3EaJUebb9Rbq+cdmrDKinMcLE602+AuUnh6I=" />
</div>
            <h3>Page FindControl Example</h3>
            <div>

            <div id="Panel1">
	
                This repeater sample shows the bubbled event and FindControl when the repeater item OnCommand event occurs.<br />
                                 <span id="Repeater1_Label1_0">RepeaterObject1</span>&nbsp;
                        <a href="javascript:__doPostBack(&#39;Repeater1$ctl00$ctl00&#39;,&#39;&#39;)">Change</a> <br />
                                     <span id="Repeater1_Label1_1">RepeaterObject2</span>&nbsp;
                        <a href="javascript:__doPostBack(&#39;Repeater1$ctl01$ctl00&#39;,&#39;&#39;)">Change</a> <br />
                                     <span id="Repeater1_Label1_2">RepeaterObject3</span>&nbsp;
                        <a href="javascript:__doPostBack(&#39;Repeater1$ctl02$ctl00&#39;,&#39;&#39;)">Change</a> <br />
                             <hr />

                This repeater shows the bubbled event and FindControl when the repeater OnItemCommand event occurs. <br />
                                 <span id="Repeater2_Label1_0">RepeaterObject1</span>&nbsp;
                        <a href="javascript:__doPostBack(&#39;Repeater2$ctl00$ctl00&#39;,&#39;&#39;)">Change</a> <br />
                                     <span id="Repeater2_Label1_1">RepeaterObject2</span>&nbsp;
                        <a href="javascript:__doPostBack(&#39;Repeater2$ctl01$ctl00&#39;,&#39;&#39;)">Change</a> <br />
                                     <span id="Repeater2_Label1_2">RepeaterObject3</span>&nbsp;
                        <a href="javascript:__doPostBack(&#39;Repeater2$ctl02$ctl00&#39;,&#39;&#39;)">Change</a> <br />
                  </div>

            </div>
        </form>
    </body>
</html>
ASPX Web Page Embedded Output:

Remarks

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.

Applies to

See also

ID

Sources and References

  • https://docs.microsoft.com/en-us/previous-versions/aspnet/ms178138(v=vs.100)100)100)
  • https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.page?view=netframework-4.8
  • https://docs.microsoft.com/en-us/previous-versions/aspnet/015103yb(v=vs.100)
  • https://docs.microsoft.com/en-us/previous-versions/aspnet/ms178472(v=vs.100)
  • https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.page?view=netframework-4.8
  • https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.page.findcontrol?view=netframework-4.8#System_Web_UI_Page_FindControl_System_String_

©sideway

ID: 211000015 Last Updated: 10/15/2021 Revision: 0 Ref:

close

References

  1. Active Server Pages,  , http://msdn.microsoft.com/en-us/library/aa286483.aspx
  2. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929%28v=vs.90%29.aspx
  3. ASP Best Practices,  , http://technet.microsoft.com/en-us/library/cc939157.aspx
  4. ASP Built-in Objects,  , http://msdn.microsoft.com/en-us/library/ie/ms524716(v=vs.90).aspx
  5. Response Object,  , http://msdn.microsoft.com/en-us/library/ms525405(v=vs.90).aspx
  6. Request Object,  , http://msdn.microsoft.com/en-us/library/ms524948(v=vs.90).aspx
  7. Server Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525541(v=vs.90).aspx
  8. Application Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525360(v=vs.90).aspx
  9. Session Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms524319(8v=vs.90).aspx
  10. ASPError Object,  , http://msdn.microsoft.com/en-us/library/ms524942(v=vs.90).aspx
  11. ObjectContext Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525667(v=vs.90).aspx
  12. Debugging Global.asa Files,  , http://msdn.microsoft.com/en-us/library/aa291249(v=vs.71).aspx
  13. How to: Debug Global.asa files,  , http://msdn.microsoft.com/en-us/library/ms241868(v=vs.80).aspx
  14. Calling COM Components from ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524620(v=VS.90).aspx
  15. IIS ASP Scripting Reference,  , http://msdn.microsoft.com/en-us/library/ms524664(v=vs.90).aspx
  16. ASP Keywords,  , http://msdn.microsoft.com/en-us/library/ms524672(v=vs.90).aspx
  17. Creating Simple ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524741(v=vs.90).aspx
  18. Including Files in ASP Applications,  , http://msdn.microsoft.com/en-us/library/ms524876(v=vs.90).aspx
  19. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929(v=vs.90).aspx
  20. FileSystemObject Object,  , http://msdn.microsoft.com/en-us/library/z9ty6h50(v=vs.84).aspx
  21. http://msdn.microsoft.com/en-us/library/windows/desktop/ms675944(v=vs.85).aspx,  , ADO Object Model
  22. ADO Fundamentals,  , http://msdn.microsoft.com/en-us/library/windows/desktop/ms680928(v=vs.85).aspx
close

Latest Updated LinksValid XHTML 1.0 Transitional Valid CSS!Nu Html Checker Firefox53 Chromena IExplorerna
IMAGE

Home 5

Business

Management

HBR 3

Information

Recreation

Hobbies 8

Culture

Chinese 1097

English 339

Reference 79

Computer

Hardware 249

Software

Application 213

Digitization 32

Latex 52

Manim 205

KB 1

Numeric 19

Programming

Web 289

Unicode 504

HTML 66

CSS 65

SVG 46

ASP.NET 270

OS 429

DeskTop 7

Python 72

Knowledge

Mathematics

Formulas 8

Algebra 84

Number Theory 206

Trigonometry 31

Geometry 34

Coordinate Geometry 2

Calculus 67

Complex Analysis 21

Engineering

Tables 8

Mechanical

Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5

Control

Process Control 1

Acoustics 19

FiniteElement 2

Natural Sciences

Matter 1

Electric 27

Biology 1

Geography 1


Copyright © 2000-2024 Sideway . All rights reserved Disclaimers last modified on 06 September 2019