Take the 2-minute tour ×

Is it possible to dynamically create an ItemTemplate for a repeater somehow?

I am trying to use a repeater since it allows the most control, but one of my requirements is making me reconsider.

I basically have a number of SQL queries that I do through a web service. Rather than having users type in the entire query I want them to be able to select "parts", "products", or "packages" via radio buttons, enter a search term in a text box, and some other info, and the page returns the results they want. I have this mostly done, the RadioButtons control logic, and I have the query set up to accept the input from the text box as a search term with wildcards. The only probelm is I am struggling with the repeater control. The problem is each one of the tables has a different number of columns and they have different names, so doing a <td><%# DataBinder.Eval(Container,\"DataItem.Description\") %></td> within the ItemTemplate is not possible(I don't know until bind time which one of the 3(possibly more in the future)templates to use)

I tried using a literal to pass in what I wanted based on logic in the codebehind, but I couldn't pass the inline functions, and I have been unable to put together how to do this based on an earlier question.

I have been reading the MSDN reference and it seems like if I learn the DataList control it will make things easier, but I'd rather not waste time on that if there's an easy way to do it with a repeater(which will also allow me more control)

Thank you

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

I may be off track here but I think an easy solution to the problem you're having would be to create multiple repeater controls with different items templates inside them and wrap them up in panels. That way depending on the user parameters you could simply databind your result to the proper repeater and set the other panels to invisible.

share|improve this answer
This worked perfectly! I experimented just wrapping a <span> element around the different templates, but that doesn't stop it from being compiled, just stops it from being shown. I also tried modifying the innerHtml property of the <span> but that didn't work either. This is a lot of extra markup, but it seems to work fine. Any idea if this causes any sort of problems down the road? –  Kyle Preiksa Jun 7 '12 at 19:59
As I've never put this into practice I'm curious to know how much markup the repeaters actually generate when they're not being bound to a datasource? I would imagine that the most it should pump out would be the Header and Footer which I can't see being a problem down the road. You could try having all of the repeaters .IsEnabled set to false and then only enable the one you need at runtime which may prevent them from being compiled and adding markup at all, but again not entirely sure. I'll try it myself tomorrow and let you know. –  Jesse Carter Jun 8 '12 at 5:14
Actually, it seems like nothing at all, there is some stuff with class="AspNetHidden" but that looks like an input of some kind, not a table. –  Kyle Preiksa Jun 8 '12 at 12:30
Sounds good to me! –  Jesse Carter Jun 8 '12 at 15:06
add comment

I was looking back through some old code for you to see how I've handled similar situations. Then I saw Jesse's answer right before I wrote my reply. Basically, I would tend to agree with Jesse there - that seems the most straightforward solution from what you've outlined.

share|improve this answer
add comment

So I don't know how much this helps, but one possibility would be to use MVC Templates. MVC is handy in that it can actually be used in a very limited sense (your entire app doesn't need to implement it, just the applicable page) and it's also pretty straightforward.

An introduction on how to do different templates within MVC: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html

It might be possible to modify the template so that it changes based on the object that it's bound to - like how a WPF DataTemplateSelector works.

I thought I remember seeing something in MVC3 or MVC4 that you could create a Template based on a datatype (like you can in Silverlight) and it would automatically pick that Template, but I'm having trouble finding that now.

share|improve this answer
add comment

Your Answer


