Template control not displaying Container properties

Discussion in 'ASP .Net Building Controls' started by fusionmaster, Jan 24, 2006.

  1. fusionmaster

    fusionmaster Guest

    I am trying to create a templated control for asp.net 2.0 using the example
    found in the documentation, however when outputing <%# Container.Message %>
    it always returns an empty string. Please help.


    control code:

    using System;
    using System.Web;
    using System.Web.UI;

    namespace TemplateControlSamples
    {

    public class TemplateItem : Control, INamingContainer
    {
    private String _message = null;

    public TemplateItem(String message)
    {
    _message = message;
    }

    public String Message
    {

    get
    {
    return _message;
    }
    set
    {
    _message = value;
    }
    }
    }

    [
    ParseChildren(true)
    ]
    public class Template1 : Control, INamingContainer
    {

    private ITemplate _messageTemplate = null;
    private String _message = null;

    public String Message
    {

    get
    {
    return _message;
    }
    set
    {
    _message = value;
    }
    }

    [
    PersistenceMode(PersistenceMode.InnerProperty),
    TemplateContainer(typeof(TemplateItem))
    ]
    public ITemplate MessageTemplate
    {
    get
    {
    return _messageTemplate;
    }
    set
    {
    _messageTemplate = value;
    }
    }

    protected override void CreateChildControls()
    {

    // If a template has been specified, use it to create children.
    // Otherwise, create a single literalcontrol with message value

    if (_messageTemplate != null)
    {
    Controls.Clear();
    TemplateItem i = new TemplateItem("this is a test");
    _messageTemplate.InstantiateIn(i);
    Controls.Add(i);
    }
    else
    {
    this.Controls.Add(new LiteralControl(this.Message));
    }
    }
    }
    }


    asp code:

    <%@ Register TagPrefix="test" Namespace="TemplateControlSamples" %>
    <test:Template1 ID="Template1_1" Message="Hello World!" runat=server>
    <MessageTemplate>
    [<%# Container.Message %>]
    </MessageTemplate>
    </test:Template1>


    the output to the browser is always []
    it sould be [Hello World!]
     
    fusionmaster, Jan 24, 2006
    #1
    1. Advertising

  2. You must do databind to get your databinding statements (<%# %>) to get
    their values. After you add your Templated item to the page, do a
    i.DataBind(). Like this:

    if (_messageTemplate != null)
    {
    Controls.Clear();
    TemplateItem i = new TemplateItem("this is a test");
    _messageTemplate.InstantiateIn(i);
    Controls.Add(i);
    i.DataBind();
    }
    else
    {
    this.Controls.Add(new LiteralControl(this.Message));
    }

    regards
    /Örvill
     
    Fredrik Örvill, Feb 1, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad

    Container.DataItem not displaying

    tshad, Aug 22, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    405
    tshad
    Aug 22, 2005
  2. Vivi Orunitia
    Replies:
    11
    Views:
    4,530
    Martijn Lievaart
    Feb 4, 2004
  3. er
    Replies:
    1
    Views:
    413
    Barry
    Sep 5, 2007
  4. Sumeet Mahajan

    User Control - Accessing Properties from the Container Page

    Sumeet Mahajan, Mar 2, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    134
    Sumeet Mahajan
    Mar 2, 2004
  5. KJ
    Replies:
    0
    Views:
    130
Loading...

Share This Page