This is a migrated thread and some comments may be shown as answers.

Finding a control 2 levels deep

3 Answers 158 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
DogEars
Top achievements
Rank 2
DogEars asked on 02 Apr 2009, 01:57 AM
I'm having some issues finding a control nested 2 levels deep.

My primary container is a RadPageView and I need to find a control inside of a Formview nested inside of that. My target is the following control:

<asp:Image ID="NPPhoto" runat="Server" CssClass="detailPhoto" />

I placed my structure in the code snippet section. I have the following code, but it's not working.

Thanks.


Dim imageURL As String
If (UserID = "" Or Not System.IO.File.Exists(Server.MapPath("~/images/profile_images/" & UserID & ".jpg"))) Then
imageURL = "~/images/notavailable.gif"
Else
imageURL = "~/images/profile_images/" + UserID + ".jpg"
End If
Dim Photo As Image = CType(NPProfile.FindControl("NPPhoto"), Image)

        Photo.ImageUrl = imageURL




<telerik:RadMultiPage ID="RadMultiPage1" runat="server" SelectedIndex="3"
            <telerik:RadPageView ID="RadPageView1" runat="server"
                <asp:FormView ID="NPProfile" runat="server"
                    <ItemTemplate> 
                    <div id="Desc_Photo"
                                <asp:Image ID="NPPhoto" runat="Server" CssClass="detailPhoto" /> 
                            </div> 
                        <div class="Desc_Container"

3 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 02 Apr 2009, 08:46 AM
Hi DogEars,

Your formview should be exposed as a codebehind field NPProfile. So you need to find a control only one level deep (the FormView).

Greetings,
Albert
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
PureCode
Top achievements
Rank 2
answered on 02 Apr 2009, 06:20 PM
One of my favorite little helper methods that has been invaluable (and once whipped up as an exercise in generics, just after they added support for that) to me as well as my business.

public static T FindControlRecursive<T>(this Control parentControl, string id) where T : Control 
    T control = default(T); 
 
    if ((parentControl is T) && (parentControl.ID == id)) 
    { 
        return (T)parentControl; 
    } 
 
    foreach (Control ctrl in parentControl.Controls) 
    { 
        control = ctrl.FindControlRecursive<T>(id); 
 
        if (control != null
        { 
            break
        } 
    } 
     
    return control; 

This will search recursively for any type of control.

Use is easy, say you are working in the code-behind of a page ('this'), and you need to find 'RadGrid1' which is several levels deep on the page somewhere:

RadGrid foundGrid = this.FindControlRecursive<RadGrid>("RadGrid1"); 

The 'this' can be replaced for anything that holds controls, 'Panel1.FindControlRecursive' for example. We use it a lot to find controls inside templates and inside user controls.

This particular version of the method is exposed as an extension method, so .NET 3.5 is required. It will nicely pop up in the intellisense for any control, right under the fairly useless 'FindControl'.

All it requires is 'System.Web.UI' to work (which contains the Control base class), for web applications or 'System.Windows.Forms' for WinForm applications.

May be useful to a few people.

Regards,

Mike

PS: If anyone has a use for a method that creates a custom pane inside the output panel of Visual Studio and allows one to write text to that on the fly (even when your application is not running, extremely handy for debugging ControlDesigners, the main reason we whipped that one up), I can post that one up as well, it is probably one of the most useful of our helper methods next to the one above. Another useful one we have is an extension method that loads UserControls from a virtual path with constructor parameters.
0
DogEars
Top achievements
Rank 2
answered on 03 Apr 2009, 02:21 AM
Hi Mike,

Thanks for that bit of code, will come in really handy, nicely done.

As for my original issue, it was resolved once I placed my findcontrol piece inside the databound event of the formview.

I'll certainly be implementing what you have written here Mike.

Thanks again.

Tags
General Discussions
Asked by
DogEars
Top achievements
Rank 2
Answers by
Atanas Korchev
Telerik team
PureCode
Top achievements
Rank 2
DogEars
Top achievements
Rank 2
Share this question
or