Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > Building a custom control with nested templates

Reply
Thread Tools

Building a custom control with nested templates

 
 
Michael Vanhoutte
Guest
Posts: n/a
 
      10-28-2004
Hi,

I'm trying to create a templated custom asp.net server control with nested
templates and I'm having some problems in getting the nesting to work.

This is an excerpt of an aspx-file that shows what I want to do:
<form id="Form1" method="post" runat="server">
<cc1:TestControl id="TestControl1" runat="server">
<ThumbnailTemplates>
<HeaderTemplate>
<b>My Thumbnail Header</b>
</HeaderTemplate>
<FooterTemplate>
<b>My Thumbnail Footer</b>
</FooterTemplate>
</ThumbnailTemplates>
<PreviewTemplates>
<HeaderTemplate>
<b>My Preview Header</b>
</HeaderTemplate>
</PreviewTemplates>
</cc1:TestControl>
</form>

My control is supposed to display an html-page containing either thumbnails
or
previews. The user will be able to switch between the two views using a
button displayed by my control.

Regardless of which one is displayed, both interfaces have the need for a
Header, a Footer and some other stuff. Since the logic and the templates
needed for both the
thumbnails and the previews are the same, I created one container (called
TestTemplatesContainer in my code below) that contains a HeaderTemplate and a
FooterTemplate-property. I was hoping to use this container inside a
<PreviewTemplates>- and <ThumbnailTemplates>-element.

So if a user chooses thumbnail-view, I would build an interface using the
templates
of the <ThumbnailTemplates>-element; if a user chooses preview-view, I would
build an interface using the templates in <PreviewTemplates>. I want to use
two different elements for this because the HTML-code that a user of my
control would specify in the aspx-file can be very different for both views.

My problem is that I now have two nested templates that I need to deal with
in my
CreateChildControls-method, but I'm not able to get the second level to
work. I would appreciate if somebody could have a look at my code below and
tell me what I'm doing wrong. I threw away most of the code and
exception-handling so that you only have relevant code.

I really would like to do it like this (as opposed to creating sub-controls)
because
my TestControl-object is the only one that can render my complete page; these
sub-controls wouldn't be able to do it.

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

namespace MyControl
{
/// <summary>
/// TestControl is my actual control.
/// </summary>
[
ToolboxData("<{0}:TestControl runat=server></{0}:TestControl>"),
ParseChildren(ChildrenAsProperties=true)
]
public class TestControl: System.Web.UI.WebControls.WebControl,

System.Web.UI.INamingContainer
{
private TestTemplatesContainer m_objThumbnailTemplatesContainer = null;
private ITemplate m_objThumbnailTemplates = null;

[
TemplateContainer(typeof(TestTemplatesContainer)),
PersistenceMode(PersistenceMode.InnerProperty),
BrowsableAttribute(false)
]
public ITemplate ThumbnailTemplates
{
get
{
return m_objThumbnailTemplates;
}
set
{
m_objThumbnailTemplates = value;
}
}

protected override void CreateChildControls()
{
m_objThumbnailTemplatesContainer = new TestTemplatesContainer();
m_objThumbnailTemplates.InstantiateIn(m_objThumbna ilTemplatesContainer);

// I was hoping that m_objThumbnailTemplatesContainer.HeaderTemplate
in the
// following test would not have been null. Unfortunately, it always is.
// What is wrong here?
if (!Object.Equals(m_objThumbnailTemplatesContainer.H eaderTemplate,
null))
{
TestHeaderTemplateContainer objContainer = new
TestHeaderTemplateContainer();

m_objThumbnailTemplatesContainer.HeaderTemplate.In stantiateIn(objContainer);

Controls.Add(new LiteralControl("<B>start"));
Controls.Add(objContainer);
Controls.Add(new LiteralControl("end</B>"));
}
else
{
Controls.Add(new LiteralControl("should never display."));
}

}
}

/// <summary>
/// TestHeaderTemplateContainer is the container used for
/// <HeaderTemplate>
/// </summary>
[
ToolboxItem(false)
]
public class TestHeaderTemplateContainer: System.Web.UI.Control,

System.Web.UI.INamingContainer
{
}

/// <summary>
/// TestTemplatesContainer is the container used for
/// <ThumbnailTemplates> and <PreviewTemplates>.
/// </summary>
[
ToolboxItem(false),
ParseChildren(ChildrenAsProperties=true)
]
public class TestTemplatesContainer: System.Web.UI.Control,

System.Web.UI.INamingContainer
{
private ITemplate m_objHeaderTemplate = null;

[
TemplateContainer(typeof(TestHeaderTemplateContain er)),
PersistenceMode(PersistenceMode.InnerProperty),
BrowsableAttribute(false)
]
public ITemplate HeaderTemplate
{
get
{
return m_objHeaderTemplate;
}
set
{
m_objHeaderTemplate = value;
}
}
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Building a control adapter for a custom control boarding_king ASP .Net Building Controls 0 09-20-2006 10:04 AM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM
ControlDesigner not invoked on custom control when control is rendered within another custom control Matt Sokol ASP .Net Building Controls 2 08-07-2003 07:13 AM
Building a custom control that implement table server control Thomas Ekegren ASP .Net Building Controls 2 07-20-2003 09:27 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57