More in this section

Forums / Developing with Sitefinity / Embedding Templates that contains registred UserControls

Embedding Templates that contains registred UserControls

4 posts, 0 answered
  1. Pierre
    Pierre avatar
    433 posts
    Registered:
    16 Feb 2006
    06 Apr 2009
    Link to this post
    Hi,
    I'm working new architecture and i have some problems to embedding my templates that contains registered Usercontrols.
    The template is created with ViewModeControl with full LayoutTemplatePath and name at backside folder. These templates have one or more single registered sub-controls. When run the application the registered expression are not identify and usercontrols of templates not working. If we need some special issue could you suggest one sample, plz.
    I have tried actually with one single work around embedding templates with no controls and after map as external. Could you suggest some better way.

    Regards
  2. Dido
    Dido avatar
    149 posts
    Registered:
    24 Sep 2012
    07 Apr 2009
    Link to this post
    Hello Pierre,

    If I understood correctly, you define controls in the template and expect to use them in a custom control the way they are used in a user control.

    If so, let me explain to you. The ASP.NET runtime automatically maps variables with the same name as control ids to the controls in the .ascx file. Visual studio does the rest of the magic with its intellisense. This is the reason why user controls are partial classes. This is when you are dealing with user controls.

    When you are doing custom controls, you don't need a template at all. It is optional, but it is a good practice, as well. The custom control's template is not a user control. It is just some markup that separates business logic from the UI layout. In a custom control, if you are using templates, it is your responsibility to load the template and then map the controls in the template to variables/properties.

    How to map a control in a template to a property?

    Since you should use the new backend architecture, you will ultimately derive from SimpleControl or ViewModeControl<THost>. They both implement a Container property, of type GenericContainer. GenericContainer has this method:
    public virtual T GetControl<T>(string id, bool required) 
    which actually is just one of the four methods with the same name. The generic argument is the type of the control you want to search for; e.g. if you have
    <asp:Label id="firstName" runat="server" /> 
    , you would search for System.Web.UI.WebControls.Label, like so:
    Label firstName = this.Container.GetControl<Label>("firstName"false); 

    The paramerers mean:
    • string id
      Id of the control in the template
    • bool required
      Whether to throw an exception if the control is not found (true), or return null (false)
    As you can see, this is very similar to System.Web.UI.Control's FindControl(string id) method, which is used often in custom data binding.

    As a conversion, here at Sitefinity, we define a public property for every server control in the template. If we follow the previous example, our property would look like:
    public ITextControl FirstName 
        get  
        {  
            return this.Container.GetControl<ITextControl>("firstName"false);  
        } 
    Here I use ITextControl, because Label implements this interface, but so does Literal. This gives me the flexibility to change the control type in the template without changing my business logic.

    I hope will find this information useful.

    Best wishes,
    Dido
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
  3. Pierre
    Pierre avatar
    433 posts
    Registered:
    16 Feb 2006
    07 Apr 2009
    Link to this post
    Hi Dido,

    Thanks but this is not the scope. I have make one very simple sample without the other linked metafields. My template work fine only not work the external UserControls registred in the template. I have tried with full path to file and same issue. The tag prefix and tagname is not well interpreted output as text in the screen.

    <%@ Register Src="Planning/gantt.ascx" TagName="gantt" TagPrefix="uc1" %> 
     
    <div class="ToolsAll">  
          
    </div> 
    <div id="divWorkArea" class="workArea insert">  
        <p> 
            <uc1:gantt id="Gantt1" runat="server"></uc1:gantt> 
        </p> 
    </div> 

    Regards
  4. Dido
    Dido avatar
    149 posts
    Registered:
    24 Sep 2012
    07 Apr 2009
    Link to this post
    Hello Pierre,

    Are you sure that the Register directive is correct? It seems to me that it should be something like:

    <%@ Register Src="~/Planning/gantt.ascx" TagName="gantt" TagPrefix="uc1" %>  

    Sincerely yours,
    Dido
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
Register for webinar
4 posts, 0 answered