Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Accessing control buried in another control
Message
From
21/09/2010 16:01:29
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
 
 
To
21/09/2010 12:11:57
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
ASP.NET
Application:
Web
Miscellaneous
Thread ID:
01482165
Message ID:
01482225
Views:
37
>>Hi all,
>>
>>I have used place holder controls fairly often for adding user controls dynamically at runtime. Typically, I can access a placeholder directly and just add a control like this.
>>
>>ctrlSpecialSpecs specialSpecControl = (ctrlSpecialSpecs)Page.LoadControl("~/ORCSelection/UserControls/ctrlSpecialSpecs.ascx");
>>           
>>// Add the Control to the Placeholder
>>this.holdSpecialSpecs.Controls.Add(specialSpecControl);
>>
>>
>>Currently I have a place holder that is inside another complex control and accessing it this way just doesn't work as the control is not visible. I tried to do a find control like this, but get a null reference exception on the control.
>>
>>mmPlaceHolder defendantHolder = (mmPlaceHolder)this.Page.FindControl("holdDefendant");
>>defendantHolder.Controls.Add(defendantDisplay);
>>
>>
>>It is defined on the page like this; very straight forward.
>>
>> <mm:mmPlaceHolder ID="holdDefendant" runat="server" >
>> </mm:mmPlaceHolder>
>>
>>
>>Anybody have any ideas on how to get access to this control?
>>Thanks
>>Tim
>
>Based on a suggestion of Paul, here is what I use (sorry, we're a VB shop)
>
>
> '*****************************************************************************
>    'Description:
>    '   Recursively searches for a control with Id property = controlID
>    '   
>    'Input Parameters: 
>    '   Control to search
>    '   controlID to search for
>    ' 
>    'Returned:  
>    '   Control or Nothing
>    '
>    'Change Log:
>    '2009.12.24 hewbxk  Defect 2955   Created  
>    '
>    '*****************************************************************************
>    Public Function FindMyControl(Of T As Web.UI.Control)(ByVal controlToSearch As T, _
>        ByVal controlID As String) As Control
>
>        Dim matchingControl As Control = Nothing
>
>        Try
>            For Each subControl As Control In controlToSearch.Controls
>                If ((Not subControl.ID Is Nothing) AndAlso (subControl.ID.EndsWith(controlID))) Then
>                    matchingControl = subControl
>                    Exit For
>
>                Else
>                    If (Not matchingControl Is Nothing) Then    'found it
>                        Exit For
>
>                    ElseIf subControl.HasControls() Then
>                        matchingControl = Me.FindMyControl(subControl, controlID)
>
>                    End If
>
>                End If
>
>            Next
>
>        Catch objEx As Exception
>            Throw
>
>        Finally
>            FindMyControl = matchingControl
>
>        End Try
>
>    End Function
>
Hi Bruce,

Thanks for the VB code, I should be able to translate that no problem. I am guessing you have this in a general use place and must pass in the page. It is a recursive call to itself which is something I have seen but never done. I will see what I can do to utilize this. Thanks!!
Tim
Timothy Bryan
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform