Custom control problem (Templated Control)

Last post 08-21-2008 5:59 PM by jkirkerx. 3 replies.

Sort Posts:

  • Custom control problem (Templated Control)

    08-18-2008, 8:46 AM
    • Member
      14 point Member
    • nkomp18
    • Member since 08-14-2008, 12:43 PM
    • Posts 8

    Hello ASP.NET community,

    I recently a control that is very simple, really. It's suppose to look like the UpdatePanel
    <asp:UpdatePanel>

    <ContentTemplate>
    Stuff
    </ContentTemplate>

    </asp:UpdatePanel>

     

    as you know where i have "stuff" we can put all bunch of controls and then initialise them in the code behind.
    In my control I do the same,

            private ITemplate tmpContentTemplate = null;
            [DefaultValue(null)]
            [Description("The content template.")]
            [TemplateContainer(typeof(ContentTemplateContainer))]
            [PersistenceMode(PersistenceMode.InnerProperty)]
            [TemplateInstanceAttribute(TemplateInstance.Single)]
            public ITemplate ContentTemplate
            {
                get { return tmpContentTemplate; }
                set { tmpContentTemplate = value; }
            }

     Notice that my ContentTemplate is TemplateInstance.Single so it's not bound anywhere and that means that all the stuff in it are accessible to the page, so for example if I do
    <my:CustomControl>

    <ContentTemplate>
    <my:OtherControl ID="x" runat="server" />
    </ContentTemplate>

    </my:CustomControl>

     

    then I would like to be able to do something like
    x.Text = "hello";
    In the code behind.

    In UpdatePanel this works fine the stuff within the template are never null but in my control they are always null, even though I'm exposing the ContentTemplate to the page, so I'm wondering what I'm doing wrong.
    I thought maybe I should be calling EnsureChildControls somewhere.. not sure what I'm doing wrong.

    Any ideas??

    Thank you very much in advance

     

  • Re: Custom control problem (Templated Control)

    08-20-2008, 11:22 PM
    • Participant
      1,966 point Participant
    • Momo_Stev
    • Member since 06-05-2007, 8:28 AM
    • Posts 224
  • Big Smile [:D] Re: Custom control problem (Templated Control)

    08-21-2008, 4:50 AM
    Answer
    • Member
      14 point Member
    • nkomp18
    • Member since 08-14-2008, 12:43 PM
    • Posts 8
    I solved it by adding an OnInit method on the control and it all worked fine. Furthermore I exposed the contents of all the template by adding : [TemplateInstanceAttribute(TemplateInstance.Single)] Thank you for the response
  • Re: Custom control problem (Templated Control)

    08-21-2008, 5:59 PM
    • Participant
      1,882 point Participant
    • jkirkerx
    • Member since 12-07-2007, 2:52 AM
    • Huntington Beach CA
    • Posts 442

     Then mark your post as resolved then

Page 1 of 1 (4 items)