Welcome to AspAdvice Sign in | Join | Help

ASP.NET: What is the function of FindControl method?

Asked on ASP.NET Forums
http://forums.asp.net/thread/1369082.aspx

What is the function of FindControl method?

I replied:

To return the control instance which has given ID. Every control has FindControl method since its available in System.Web.UI.Control class.

FindControl works in two ways, it can take in the local ID of the control (exactly as is given to ID attribute on aspx) when it looks for the control in the current naming container. Other way is to give UniqueID of the control to FindControl method, when control is located deeper in the naming hierarchies (for example in a GridView), when FindControl resolves the control from there and returns reference to it.

For example, you have a TextBox on Page.

<asp:TextBox ID="TextBox1" runat="server" />

You could reference it in code:

Dim tb As TextBox = CType(Page.FindControl("TextBox1"), TextBox)

In practise this is obsolete code, since control on Page level has member automatically (in ASP.NET 2.0 in any case, in ASP.NEt 1.x in inline code and in codebehind with explicit member declaration) but would work. Just typing TextBox1.xxx, where xxx stands for the member of the TextBox type, would also work.

Other example is having TextBox in a FormView as FormView is a naming container (implements INamingContainer) which essentially means that FormView provides its own naming scope for its child controls. It is important to note that by default FindControl locates controls from the current naming scope. So to look from FormView's naming scope, you run FindControl against FormView instance.

<asp:FormView ID="FormView1" runat="server" DefaultMode="ReadOnly"  >
        <ItemTemplate>
            <asp:TextBox ID="TextBox1" runat="server" Text="Text in TextBox" />
        </ItemTemplate>
    </asp:FormView>
    <asp:Button ID="Button1" runat="server" Text="Let's look for the TB" />

And code for it:

 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        'Looking from FormView's naming scope - FormView implements INamingContainer
        Dim tb As TextBox = CType(FormView1.FindControl("TextBox1"), TextBox)
        Response.Write(tb.Text)

        'Method 2. UniqueID - rendered as name attribute in markup - in this case running from Page works
        tb = CType(Page.FindControl("FormView1$TextBox1"), TextBox)
        Response.Write(tb.Text)

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            'Dummy binding tom FormView to make it build control hierarchy
            Dim arr As New ArrayList()
            arr.Add(1)
            FormView1.DataSource = arr
            FormView1.DataBind()
        End If
    End Sub

In either of cases, TextBox is found within FormView and it's Text is printed on output

Sponsor
Published Saturday, August 12, 2006 6:13 PM by joteke
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: ASP.NET: What is the function of FindControl method?

Someone may want to perform a recursive search in the page object: http://dotnetslackers.com/community/blogs/simoneb/archive/2006/07/03/145.aspx
Sunday, August 13, 2006 6:41 AM by Simone

# An Ordinary Developer&#8217;s Rants &raquo; Blog Archive &raquo; Finding Controls

# How to communicate between users controls in web form? | keyongtech

# re: ASP.NET: What is the function of FindControl method?

Great posting Joteke. Here is the same basic thing, but I am using C#. Check it out: http://justgeeks.blogspot.com/2009/05/using-findcontrol-to-find-your-control.html
Monday, May 04, 2009 4:57 PM by Brent V

# Amoxicillin dosage for greyhound sinus infection.

Amoxicillin clavulanate potassium. Amoxicillin. Amoxicillin dosage. Amoxicillin allergy itch. Amoxicillin not pink.

Saturday, June 13, 2009 10:30 AM by Amoxicillin.

# i have one page that contain textbox and i have to invisible the textbox from another page

can you solve?
Saturday, August 01, 2009 2:01 AM by sachin shah

# Sell S320 Private, S320 Replacement Parts Window Regulator 1999 Mercedes Benz

# E420 Oem Exterior, E420 Radiator Replacement 1995 Mercedes Benz

# Ls1 Performance Spare Parts, Ls1 Parts Cylinder Head Casting Numbers Camaro Z28

# 1997 Saturn Sc1 Vin Delco, Sc1 Part Saturn Sw2 Strut Bar

# Used Mark Vi Mkx Lincoln Continental, Used Mark Vii Mkx Lincoln Blackwood

# C43 Discount Latest Mercedes C36 Amg, C36 Boiler C32 Amg

# Legend Used Get Lifted Lyrics, Swiss Legend White Ceramic Watch Black Mother Of Pearl

# 1989 Chevrolet Cavalier, Chevrolet Cavalier 2 Door

Friday, May 21, 2010 8:38 AM by 1989 Chevrolet Cavalier, Chevrolet Cavalier 2 Door

# Catalina Tour Package, Catalina Sheet Music

Friday, May 21, 2010 8:55 AM by Catalina Tour Package, Catalina Sheet Music

# 1987 Montero Sale, Buy Monte Carlo Georgia

Friday, May 21, 2010 8:55 AM by 1987 Montero Sale, Buy Monte Carlo Georgia

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below