Kyle Preiksa
Kyle Preiksa

Reputation: 514

Creating Repeater Template at runtime and based upon user input

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

Upvotes: 2

Views: 759

Answers (3)

Rob Horton
Rob Horton

Reputation: 825

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.

Upvotes: 2

Jesse Carter
Jesse Carter

Reputation: 21147

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.

Upvotes: 2

BoB
BoB

Reputation: 123

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.

Upvotes: 1

Related Questions