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

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?

Thanks,
Paolo


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)
            
            'textbox
            If TypeOf ctrl Is TextBox Then
                Dim txt As TextBox
                txt = CType(ctrl, TextBox)
                txt.Text = in_Value
            End If
         
            'hiddenfield
            If TypeOf ctrl Is HiddenField Then
                Dim hdn As HiddenField
                hdn = CType(ctrl, HiddenField)
                hdn.Value = in_Value
            End If
         
            'listbox
            If TypeOf ctrl Is ListBox Then
                Dim cmb As ListBox
                cmb = CType(ctrl, ListBox)
                cmb.SelectedValue = in_Value
            End If
         
            'label
            If TypeOf ctrl Is Label Then
                Dim lbl As Label
                lbl = CType(ctrl, Label)
                lbl.Text = in_Value
            End If
    
            'checkbox
            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

crickpaoloAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ororioleCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
crickpaoloAuthor Commented:
Works Great!  Thanks man.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.