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:

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
================================================== =========


All times are GMT -4. The time now is 02:44 AM.

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