1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / base.Container.GetControl

base.Container.GetControl

4 posts, 0 answered
  1. Bobby
    Bobby avatar
    56 posts
    Registered:
    11 Feb 2009
    29 Jun 2010
    Link to this post
    Hi,


    I am trying to use the base.Container.GetControl function to get a control from my custom template.At first I wanted to place my custom control and logic in a user control and access that user control from my custom template. However, when I used base.Container.GetControl to get my user control the following error was returned,

    A required control was not found in the template for "ResearchAreas.Resources.ControlTemplates.Backend.ResearchAreaLibraryItemEdit.ascx". The control must be assignable form type "System.Web.UI.Control" and must have ID "myID".


    My custom control only had a RadTreeView control in it. So, I then decided to move my code from my user control and place it directly in my custom template. Now when I looked for the RadTreeView directly, the function, base.Container.GetControl, found the RadTreeView and everything worked as expected. So, next, out of curiosity, I looked for a Label in my custom template by the id and the following error was thrown,

    A required control was not found in the template for "ResearchAreas.Resources.ControlTemplates.Backend.ResearchAreaLibraryItemEdit.ascx". The control must be assignable form type "System.Web.UI.Control" and must have ID "testLabel".



    So, my question is this, does the base.Container.GetControl function only look for RadControls or is it something special that I need to do to get the function to find controls that are not telerik controls. Below is a sample of my custom template and how I call base.Container.GetControl

    Custom Template:
    <fieldset class="set">
                                <ol class="setIn">
                                    <li class="catSel clearfix">
                                        <asp:Label ID="testLabel" runat="server" />
                                        <asp:Label ID="Label8" AssociatedControlID="Category" runat="server">
                                            <asp:Literal ID="Literal1" runat="server" Text="<%$Resources:ItemEdit_Category %>"></asp:Literal>
                                        <em id="Em1" runat="server"></em></asp:Label>
                                        <sf:ContentCategoriesField ID="Category" runat="server" Visible="false" />
                                        <telerik:RadTreeView ID="treeViewItemResearchAreas" runat="server"></telerik:RadTreeView>
                                    </li>
                                </ol>
                            </fieldset>

    Class code:
    RadTreeView tree = base.Container.GetControl<RadTreeView>("treeViewItemResearchAreas", true);
    Control c = base.Container.GetControl<Control>("testLabel", true);

    base.Container.GetControl finds the tree but throws an error for the label. Any help would be greatly appreciated.

    Thanks,
    Bobby
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    29 Jun 2010
    Link to this post
    Hello Bobby,

    You have to specify the type

    Label c = base.Container.GetControl<Label>("testLabel", true);

    or make a cast

    (Label)base.Container.GetControl<Control>("testLabel", true);

    base.Container.GetControl looks recursively for all controls inside a template. The type of the controls does not matter.

    Greetings,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Bobby
    Bobby avatar
    56 posts
    Registered:
    11 Feb 2009
    29 Jun 2010
    Link to this post
    Hi again,

    I believe I've found the answer, I guess I was using the wrong overload of the base.Container.GetControl function

    base.Container.GetControl<Control>("myUserControlID", true, TraverseMethod.BreadthFirst);

    found my custom user control
  4. Bobby
    Bobby avatar
    56 posts
    Registered:
    11 Feb 2009
    29 Jun 2010
    Link to this post
    Thanks Ivan, I didn't realize you replied until after I posted the second time.
Register for webinar
4 posts, 0 answered