Header, Footer, Content Templated Control

Discussion in 'ASP .Net Building Controls' started by jacobya, Sep 19, 2003.

  1. jacobya

    jacobya Guest

    I realize this question has been asked in many different newsgroups,
    but reading through these discussions and the vague examples that come
    with the documentation, I have still not got a good idea on the best
    approach for coming up with some type of templating solution. I have
    looked at the Master Pages approach and I personally think it is too
    cumbersome and confusing. I would like to have a design-view friendly
    solution that junior developers can use quickly and easily. I'm
    currently investigating the templated control approach and what I
    would like to have is the following:

    _______________
    |
    | Static Header
    |______________
    |
    | Drag and Drop
    | Content Area
    |______________
    |
    | Static Footer
    |______________

    My question is this: is a templated control the best approach for
    accomplishing this, and will it allow me to maintain a drag-and-drop,
    design-view friendly content area (that allows for GridLayout or
    FlowLayout). If so, does anyone have any code samples of how this can
    be done that they'd be willing to share? Below is the code that I
    have so far, but it's still nowhere near where it needs to be.

    // Master container control that will hold header, footer and content
    controls

    using System.ComponentModel;
    using System.Web.UI.Design;
    using System.Collections;
    using System.Web.UI.WebControls;


    namespace CustomControls
    {
    [Designer(typeof(ReadWriteControlDesigner))]
    [ParseChildren(true)]
    [PersistChildren(true)]
    public class TemplateControl : Control, INamingContainer
    {
    public HeaderControl header;
    public ContentControl content;
    public FooterControl footer;

    public TemplateControl()
    {}


    public String Text
    {
    get
    {
    return header.Text + "<br>" + content.Text + "<br>" + footer.Text;
    }
    }

    }


    // ****************** Header Control **********************

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

    namespace CustomControls
    {
    [ParseChildren(true)]
    public class HeaderControl : Control, INamingContainer
    {
    private ITemplate template;
    private String text = "Header";
    private Control myTemplateContainer;

    protected override void OnDataBinding(EventArgs e)
    {
    EnsureChildControls();
    base.OnDataBinding(e);
    }


    [TemplateContainer(typeof(TemplateControl))]
    public ITemplate Template
    {
    get
    {
    return template;
    }
    set
    {
    template = value;
    }
    }

    public String Text
    {
    get
    {
    return text;
    }
    set
    {
    text = value;
    }
    }


    public Control MyTemplateContainer
    {
    get
    {
    return myTemplateContainer;
    }
    }

    protected override void CreateChildControls ()

    {
    if (Template != null)
    {
    myTemplateContainer = new TemplateControl();
    ((TemplateControl)MyTemplateContainer).header = this;
    Template.InstantiateIn(myTemplateContainer);
    Controls.Add(myTemplateContainer);
    }
    else
    {
    Controls.Add(new LiteralControl(Text));
    }
    }

    }
    }


    // ****************** Content Control **********************

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;
    using System.Web.UI.Design;

    namespace CustomControls
    {

    [Designer(typeof(ReadWriteControlDesigner))]
    [ParseChildren(true)]
    [PersistChildren(true)]
    public class ContentControl : WebControl
    {
    private ITemplate template;
    private String text = "Content";
    private Control myTemplateContainer;

    public ContentControl()
    {}

    protected override void OnDataBinding(EventArgs e)
    {
    EnsureChildControls();
    base.OnDataBinding(e);
    }


    [TemplateContainer(typeof(TemplateControl))]
    public ITemplate Template
    {
    get
    {
    return template;
    }
    set
    {
    template = value;
    }
    }

    public String Text
    {
    get
    {
    return text;
    }
    set
    {
    text = value;
    }
    }



    public Control MyTemplateContainer
    {
    get
    {
    return myTemplateContainer;
    }
    }

    protected override void CreateChildControls ()

    {
    if (Template != null)
    {
    myTemplateContainer = new TemplateControl();
    ((TemplateControl)MyTemplateContainer).content = this;
    Template.InstantiateIn(myTemplateContainer);
    Controls.Add(myTemplateContainer);
    }
    else
    {
    //Controls.Add(new LiteralControl(Text));
    }
    }

    }


    }


    // ****************** Footer Control **********************

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

    namespace CustomControls
    {
    [ParseChildren(true)]
    public class FooterControl : Control, INamingContainer
    {
    private ITemplate template;
    private String text = "Footer";
    private Control myTemplateContainer;

    protected override void OnDataBinding(EventArgs e)
    {
    EnsureChildControls();
    base.OnDataBinding(e);
    }


    [TemplateContainer(typeof(TemplateControl))]
    public ITemplate Template
    {
    get
    {
    return template;
    }
    set
    {
    template = value;
    }
    }

    public String Text
    {
    get
    {
    return text;
    }
    set
    {
    text = value;
    }
    }



    public Control MyTemplateContainer
    {
    get
    {
    return myTemplateContainer;
    }
    }

    protected override void CreateChildControls ()

    {
    if (Template != null)
    {
    myTemplateContainer = new TemplateControl();
    ((TemplateControl)MyTemplateContainer).footer = this;
    Template.InstantiateIn(myTemplateContainer);
    Controls.Add(myTemplateContainer);
    }
    else
    {
    Controls.Add(new LiteralControl(Text));
    }
    }

    }
    }

    Many thanks and sorry for the long post.
     
    jacobya, Sep 19, 2003
    #1
    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. John Crowley
    Replies:
    2
    Views:
    485
    =?Utf-8?B?Sm9obiBDcm93bGV5?=
    Feb 6, 2004
  2. abc
    Replies:
    1
    Views:
    633
    Shaun McDonnell
    Apr 30, 2006
  3. Replies:
    0
    Views:
    2,238
  4. Replies:
    7
    Views:
    788
    Alan Silver
    Aug 1, 2006
  5. Henrik
    Replies:
    1
    Views:
    432
    Ken Cox [Microsoft MVP]
    Jul 6, 2006
Loading...

Share This Page