views:

203

answers:

1

I find the repetition of sets of controls for each of the EditItemTemplate, InsertItemTemplate, and ItemTemplate templates of a FormView to be tedious and risky, in terms duplicating layout and code etc. I would much rather create a xxxDetails user control, and use this in each template, cutting layout and code location down to one location.

However, this introduces several complexities for data binding scenarios. Are there any extablished patterns or practice guides for using user controls in these scenarios?

+2  A: 

Microsoft just added really great support for this in ASP.NET 4.0. It is called DynamicData. They added methods on all data controls called EnableDynamicData(type). There are default templates included in ASP.NET, but you can make your own.

To use built custom templates, just add a DynamicData/Templates directory in your folder. Inside it you can add type views insert and edit templates for all of your data types. The default templates have validators built in so they are a great starting point!

Here is a sample I put together for using custom controls in DynamicData templates. I used a slider for editing integers, and CurrencyInput for money etc...

If you can go with using ASP.NET 4.0 I would highly recommend doing this. Also, even if you can't, you still might want to use the pre-built templates as guidance.

Banzor
I love Dynamic Data, and it's been around since before ASP.NET 4.0, but it doesn't solve my problem. You are talking about page templates, I'm talking about FormView templates possibly used on page templates.
ProfK
No, I am talking about templates used in FormView items. This is a new WebForms feature added in ASP.NET 4.0. This is not the DynamicData that you are thinking of from 3.5 (which is page templates). Please run the sample to see what I am suggesting. You can use it in Web Application Projects and Website Projects! I think you will really find it to be useful. Its sole purpose was to address the issue you raised which is too much copy and pasting and too much abuse of TemplateItems.
Banzor