Modifying template at design time (ITemplate)

bri189a

Senior Contributor
Joined
Sep 11, 2003
Messages
1,004
Location
VA
I have a header in a repeater control that requires changing at run time.

Based upon what the user does I will need to pick from three different headers, and three different Item/AltItem templates. The reason being that while the majority of the columns will stay the same, a couple will need to removed to make way for the couple that will be shown in it's place because of the paritcular option the user has chosen.

I created a subclass for each header and Item that implements ITemplate. In the overloaded method for that interface I draw the template for that item. That works great for static text. The problem comes on the text that depends on server variables.

For instance in the header template for the default (which is in the html):

<th><%# IsOwner ? "Edit" : "Status" %></th>

Now in my subclass I have:

RepeaterItem r = new RepeaterItem(RepeaterType.Header); //whatever it is
r.Controls.Add(new LiteralControl("<th><%# IsAdmin ? \"Edit\" : \"\" %></th>"));
container.Controls.Add(r);

Obviously, despite my lack of remembering the exact class, that doesn't work because it's a literal control. But you see what I'm trying to do. I tried changing my sub class to inherit TemplateBuilder rather than implement ITemplate, but this only results in nothing being drawn at all. TemplateBuilder implements ITemplate and would therefore work for setting the template, but it's not working for me.

If you are going to suggest that I create 3 sepearte repeaters then I've already thought of that and I don't consider setting visibility on 2 seperate repeaters and showing 1 good coding practice. It works, but as a friend of mine says, it has a bad smell. Plus now I have to wade through a lot more code in the html to find things.

I've considered creating 3 UserControls that just have the repeaters as their only control and loading it at runtime based upon what I need, but I'm trying to avoid that if possible.

I've also thought of displaying all data and hiding the columns I don't need, but that has that same bad smell as hiding the whole repeater, so I don't want to do that either. There is a way to set Header and Item templates at runtime, I'm doing it now, it's just how to do it when those templates request server variables and data that I'm having issues with (in other words when the HTML template would have <%# %> tags in it).

Thanks for your help.
 
Back
Top