p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 2.0 Professional (http://p2p.wrox.com/forumdisplay.php?f=215)
-   -   Passing Templates in a Composite Control (http://p2p.wrox.com/showthread.php?t=67647)

dparsons April 24th, 2008 12:28 PM

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:


    ToolboxData("<{0}:CustomContainerControl runat=\"server\"></{0}:CustomContainerControl>")]
public class CustomContainer : CompositeControl, INamingContainer
   private ITemplate containerItemTemplate;
   private Control containerItemControl;
   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;

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:


<cc1:CustomContainerControl ID="container" runat="server">
<tr><td bgcolor="#ffffff"><%#Eval("Foo")%></td></tr>

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!


================================================== =========
Read this if you want to know how to get a correct reply for your question:
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========

All times are GMT -4. The time now is 09:14 AM.

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