Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 24th, 2008, 12:28 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default Passing Templates in a Composite Control

Hello Everyone,
   This may be a bit difficult to explain but I am going to try. I wrote a composite control that consists of various controls but the control of importance here is a custom repeater control that I wrote. Now, if I were to drop this Repeater control onto a web form I can easily supply it with a Template (Header, Item, Footer) just as I would with the standard Repeater control.

I ran headlong into a wall, however, when I placed this Repeater control inside of my Composite control since there was no apparent way for me to supply a template on the WebForm that would then be passed to my Repeater Control.

After rigorous searches on Google the only thing that I could turn up that was worthwhile was how to implement Templates in custom controls that inherited from a control like the Repeater, GridView, etc which I have already done inside of my Repeater control. So what I ended up doing was something like this:

Code:
[DesignerAttribute(typeof(CustomContainerDesigner)),
    ToolboxData("<{0}:CustomContainerControl runat=\"server\"></{0}:CustomContainerControl>")]
public class CustomContainer : CompositeControl, INamingContainer
{
   private ITemplate containerItemTemplate;
   private Control containerItemControl;
  [Browsable(false),PersistenceMode(PersistenceMode.InnerProperty)]
   public virtual ITemplate ItemTemplate
   {
      get { return containerItemTemplate; }
      set { containerItemTemplate = value; }
   }
   protected override void CreateChildControls()
   {
     CustomRepeater rpr = new CustomRepeater();
     if (ItemTemplate != null){ ItemTemplate.InstantiateIn(containerItemControl); }
     rpr.ID = "rpr";
     rpr.ItemTemplate = ItemTemplate;
     Controls.Add(rpr);
   }
}
I obviously have properties that deal with Header and Footer Items that I have omitted here. By using the above code I am able to achieve something like this:

Code:
<cc1:CustomContainerControl ID="container" runat="server">
<HeaderTemplate>
<table>
<tr><td>Foo</td></tr>
</HeaderTemplate>
<ItemTemplate>
<tr><td bgcolor="#ffffff"><%#Eval("Foo")%></td></tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</cc1:CustomContainerControl>
Everything works as I want it to: everything defined in the above template is passed to my Repeater control and is displayed correctly. The problem is I can't help but feel that this is the "short cut" way of doing things and that a more efficient , albeit complex, way exists.

Any opinions and/or critiques are welcome!

-Doug

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chp.5 composite control properties studen77 BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 1 October 6th, 2006 03:35 PM
Composite Control Part 2 origjones General .NET 2 September 18th, 2006 03:28 AM
Building composite Control with Image + DDL mehdi_tn ASP.NET 2.0 Professional 1 April 12th, 2006 10:48 PM
PLEASE HELP - Composite Control brettk_1 ASP.NET 1.0 and 1.1 Professional 0 August 3rd, 2005 06:22 AM
Composite Control bmains .NET Framework 2.0 0 July 25th, 2005 12:57 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.