Browse DevX
Sign up for e-mail newsletters from DevX


Showing Some MVP Love : Page 2

Microsoft changed the rules for the MVP program this year, which is great so long as they remember to trim the fat. Also, learn how to make cleaner reports by removing redundant data using the Repeater control.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Repeat Offender
Figure 1: A lot of redundant data.
I often find myself in situations where I need to generate simple reports for an ASP.NET Web application. Sometimes I use a DataGrid control, but I usually end up using a Repeater because it offers ultimate flexibility. One common problem I encounter while creating reports is how to effectively deal with the display of duplicate data items. For instance, a report of the orders for Canada contained in the Northwind database displayed in a simple Repeater might look like Figure 1. You can see that there is a lot of redundant data in the Employee Name and Company Name fields. You could read the report much more easily if you group the data logically with redundant data stripped out, as in Figure 2. The trick to removing duplicate items from templated controls lies in keeping track of which item is being currently rendered and comparing it with the previous item to look for duplicate data. This trick is pretty difficult to manage using just embedded template code in your ASPX page, so it is much easier to call from your Repeater control's ItemTemplate into a generic GetNoRepeatField function exposed by your code-behind class (see Listing 1).

Figure 2: The redundant data is stripped out.
The GetNoRepeatField function accepts two parameters. The fieldIndex parameter specifies where the value parameter belongs in the report's hierarchy (see Listing 2). You need to declare a static string array named "values" with as many elements in it as you are going to track in your report hierarchy. In the case of the Orders report, the code strips out duplicate data items for the first two columns, Employee Name and Company Name. We'll initialize the string array with two items. You need a static array so that it keeps its state across multiple calls to the GetNoRepeatField function. Next you need to determine if the item being passed into the function is the same as the item stored in the "values" string array. If it is, you'll return an empty string out of the function. If the value is not a match, then you need to store the new value in the "values" string array. Then you'll blank out each element in the "values" string array that has an index greater than the one you're currently working with. Why do this? Because if a parent level of a report changes, then all children of that report level need to be reset. Finally, return the value passed into the function as its result so that it gets displayed to the screen.

As you can see, this simple technique enables you to eliminate all the duplicate data elements in your ASP.NET reports. It's also applicable to DataGrids or any of the other templated server controls, if you prefer to use them. Once you put this code into action, the end users of your Web application will definitely appreciate the cleaner, more elegant appearance of your reports.

Jonathan Goodyear is the president of ASPSoft, Inc., an Internet consulting firm based in Orlando, FL. He is a Microsoft Certified Solution Developer and is the author of "Debugging ASP.NET" (New Riders). He is also a contributing editor for Visual Studio Magazine and asp.netPRO Magazine, and speaks frequently at major technology conferences such as VSLive and ASP.NET Connections. Reach him at through his angryCoder eZine.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date