Repeated code in databound controls - asp.net

I'm sure I'm missing something obvious here.
I have a about twenty data-bound GridViews, each of which share some common features. Let's say they're all lists of people, and every one starts with about 10 columns which are complex linkButtons with hovers etc, they're all the same. The remainder of these grids, which are extremely wide, consist of completely different columns; it's just the first 10 (of about 100) columns which are duplicated.
Hence I have this chunk of code which handles those 10 database columns and renders that html out, using 10 Eval() statements etc. It's the same code in each GridView, it's repeated 20 times over and that's obviously not cool.
I can't just chuck it in a user control or a custom control because I need the GridView to do the column headers and all that stuff. I can of course put a user control in for one column and render everything I want that way ("eval" works nested like that), but it doesn't give me the column headers, which are also complex.
So that's the question: is there any way I can break out of a templated GridView control to abstract this repeated code? I almost want a sort of server-side include or pre-processed thing, as I just need to repeat the text really, but I need to do it before asp.net tries to compile it. I could use a custom control to render any HTML I like, but that doesn't solve the problem as it's asp.net code I need to generate, not HTML.

Can't you subclass from Gridview and implement the common functionality for all the 10 gridviews there and then have further subclasses that implement the specific functionality for the specific gridviews? I've done this successfully with Pages (the most base class had authentication logic that all subclasses inherited)

Related

Keeping ASP.NET Webforms templates DRY

I've inherited an ASP.NET Webforms applications that deals with numerous similar views of data. However, because of the templates and because it uses the control which also has separate templates for various modes (EditItemTemplate, ItemTemplate, etc.) I have several pages that have numerous repetitive definitions of how to display each piece of data.
EDIT: For clarity, the duplications are of small chunks of the overall UI, not duplication of the entire UI. A column definition in a subtable of a grid inside a formview can get repeated 4 or more times (Both the ItemTemplate and the EditItem Template of the FormView would have both ItemTemplates and EditItemTemplates for the particular master table volumn.)
(Needles to say this makes making and verifying changes drastically harder than it should be.)
If this was XAML, I could declare the repetitive parts as Resources and reuse them to achieve a level of DRYness. (Don't Repeat Yourself)
I've made some code snippets for adding new items to keep additions consistent, but it doesn't help with making sure edits happen everywhere consistently.
Are there any tools to help with this in ASP.NET Webforms or am I stuck with WET code? (Actually, Write Everything Twice is a understatement for this situation. Some pages have up to 8 repetitions of the same UI for columns)
If you have the exact same UI, then I would question why you need multiple controls in the first place.
If they truly are that identical, then why not consolidate the controls into a single control and use Object oriented techniques to alter the code implementation that uses the single control in the code behind.
For example, you might have a dictionary of Action delegates that all use the same UI based on parameters a different implementation gets selected. If you do this correctly (say, using an MVP pattern) then you can make this highly testable and create unit tests to test your different implementations.

ASP.NET Design Issue: what is the best asp.net control for creating a table with 15 properties?

I am a new ASP.NET developer and now I need to develop a data entry system with 15 fields that allow the admin of the system to enter some data under each one of these 15 properties. From what I learned in ASP.NET tutorial, I think the best control is the ListView control in order to give the admin of the system to enter a new field if he wants in the future. My problem now is the following:
How to divide these 15 fields into two columns? Because all what I see about using ListView is putting all of the properties at the first row and the entries will be underneath of them. What I want is to create a list with two columns of properties and two columns for the entries
I'm not sure that a ListView makes sense from your description. Are your administrators updating the same properties on different objects? Like most of the ASP.NET controls in its class -- the Repeater and GridView are other similar examples -- the ListView is meant to create several rows of identical information based on an HTML template you provide.
If your administrators are creating or updating:
The same property for different objects, then the ListView, Repeater, or GridView would be fine.
Different properties for the same or different objects, then you need a more traditional form with normal data entry controls (like the TextBox, HtmlSelect or DropDownList, CheckBox, etc.)
I would use a more liberate approach and code the fields my self into the view you'd like.
At first it might take more time, but after that your would be able to control the look of it much quicker and easily.
Using a listView you can build your edit/input gui as you'd like - a table or any other way. If really must divide into 2 columns - you could just put a label and then the field just underneath it. 7 on one side and 8 on the other.
If you're looking to use the edit features of a data control, you could create a custom item template for your data control (be it a gridview or whatnot) that has a table and each of the properties (I assume all 15 are conisidered 1 row of data). Bind your single row to the data control and it should work. You could even have different template views for display and editing using the templates.

FormView or not?

I have an ASP.NET page with a Wizard control containing several pages of form fields. The data is collected and inserted to a database from the code behind page. I need to set this form up so you can not only insert, but edit a record as well. Since the form is long and complex, I would rather use the existing one and not make a duplicate one for editing, especially since I want to keep both forms exactly the same and any edits would have to be made to both. But it looks like this is what I need to do if I'm going to databind it. But this would also involve putting the Wizard inside of a FormView, and then I'd have to use FindControl to access any of the fields which would mean altering all my already-existing code (which of course would be time-consuming). So should I manually enter all the values from the code behind instead of databinding it? Which is better, to use a FormView and have duplicate forms (plus have to go in and redo the way I access the fields), or to do everything from the code behind?
I cheat in this circumstance. :)
Create each screen as 2 separate user controls
One for edit, and one for view
Then you get access to all you usual coding
Then embed the controls into the Wizard/FormView
I would suggest you to Go Ahead using FormView, as using DataBind control you have more control the functionality and layout Insert/Edit/View template. Since you have specified that your form is very complex and long, if you control from code behind you have to do lot of work to handle this in code behind and lot code required.
Since I have personal experience to develope very complex form using FormView and it was easy for me bind the Value in directly in formview instead if you assign/Get Values of each conrol in code behind and sometimes you have to hide.

Dynamically Created Controls or Static Controls that are Hidden or Shown

I've got a set of ASP.Net pages that display a number of asp:TextBox fields depending on the number of entries in a configuration file. I know that the number of fields won't be going above 10 or so. Given that, should I declare a sufficiently large number of text boxes in markup, or should I dynamically create the textboxes in the code-behind?
There are advantages and disadvantages to each approach, which is why I'm having trouble choosing. The advantage to the dynamic approach is that the application is more flexible - even if the number of fields goes above 10, my application will be able to scale. The disadvantage is that I'm now mixing markup and logic - my application is inserting textboxes and literals (for the labels) into the page. This will make future maintenance harder because not all of the fields are in the .aspx file.
The advantage to declaring a large form and then just showing or hiding the necessary fields is that it keeps logic and markup separate. The disadvantage, of course, is that I lose flexibility. If the number of fields goes beyond the amount I anticipate (and there is a small risk of that) I have to revisit the application to add more fields.
So, StackOverflow, which would you choose? More importantly, why would you choose your approach?
One option is to combine the best of both worlds. If you put your textbox in a repeater, you can then dynamically control how many repeater rows are displayed, while having the controls statically declared.
I would definitely go with the dynamic approach because, as you say, this will scale if requirements change in the future and you will not be sending unnecessary markup to the browser.
I don't see how using static fields avoids the issue of mixing logic and markup as you will need to use code to hide the unwanted controls.

Rendering different repeating data

What is the best method to display repeating data with slightly different controls or sub data? For example displaying an expanded list of questions and answers. Some questions will have answers, some will not. Some button controls would show for some items while not for others.
In classic ASP I've used XML/XSL quite effectively for displaying data in this manner. In .NET I've used functions being called from a label with the HTML writer class to render controls dynamically and nested list views that would bind or not depending on existing data.
I know I could also use XSL with .NET but my question is - is there a better method of displaying data this way? Inline IIf's and functions being called from the front end doesn't seem very clean.
I think the ListView would work in your situation:
http://blogs.msdn.com/mikeormond/archive/2008/01/23/some-asp-net-3-5-listview-control-examples.aspx
http://weblogs.asp.net/scottgu/archive/2007/08/10/the-asp-listview-control-part-1-building-a-product-listing-page-with-clean-css-ui.aspx
You have the ability to group items in different layouts using the ItemTemplates and you can also tap into the event handlers when loading the data.
EDIT:
You may be able to do it this way dynamically, but it's tricky with the ID's:
http://blogs.msdn.com/mikeormond/archive/2008/07/26/dynamically-loading-listview-templates.aspx
Or you could simply create a table composite control and build it up dynamically yourself...

Resources