View Single Post
  #1 (permalink)  
Old April 24th, 2008, 12:28 PM
dparsons dparsons is offline
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:

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