How do I use a FindControl method inside a Web User Control?

Posted on 2008-11-17
Last Modified: 2012-05-05
Hi Experts,

I have a subroutine that finds a web control, then sets its value. (Please see attached code snippet)  

However, the FindControl cannot find the control if I put it on a user control, which I then include on a page.  

If I put this subroutine inside the page, instead of the user control, the subroutine works.

How do I find controls in a page from inside a user control?


Public Sub Set_Form_Field(ByVal in_ControlID, ByVal in_Value)

        'sets the value of the form field regardless of web control type. 

        If Not IsNothing(FindControl(in_ControlID)) Then

            Dim ctrl As Control

            ctrl = FindControl(in_ControlID)



            If TypeOf ctrl Is TextBox Then

                Dim txt As TextBox

                txt = CType(ctrl, TextBox)

                txt.Text = in_Value

            End If



            If TypeOf ctrl Is HiddenField Then

                Dim hdn As HiddenField

                hdn = CType(ctrl, HiddenField)

                hdn.Value = in_Value

            End If



            If TypeOf ctrl Is ListBox Then

                Dim cmb As ListBox

                cmb = CType(ctrl, ListBox)

                cmb.SelectedValue = in_Value

            End If



            If TypeOf ctrl Is Label Then

                Dim lbl As Label

                lbl = CType(ctrl, Label)

                lbl.Text = in_Value

            End If



            If TypeOf ctrl Is CheckBox Then

                Dim chk As CheckBox

                If in_Value = "True" Then

                    chk = CType(ctrl, CheckBox)

                    chk.Checked = True

                End If

            End If

        End If


    End Sub

Open in new window

Question by:crickpaolo
    LVL 16

    Accepted Solution

    The FindControl method searches only inside its container by default. If its in a User Control, the User Control is its container, so it won't look outside the User Control.

    You can tell it which container to search. In this case you want the Page, so you call Page.FindControl("blah") and it will find "blah" on the Page.

    Author Closing Comment

    Works Great!  Thanks man.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now