Link to home
Start Free TrialLog in
Avatar of crickpaolo
crickpaolo

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of ororiole
ororiole
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of crickpaolo
crickpaolo

ASKER

Works Great!  Thanks man.