Simplifying ASP.NET ListView Control Templates by Dan Wahlin

Home > Blogs > Developer Visual Studio / ASP.NET > Simplifying ASP.NET ListView Control Templates by Dan Wahlin

Simplifying ASP.NET ListView Control Templates by Dan Wahlin

Like This Blog 0 Dan Wahlin
Added by February 15, 2008

I've been working with the new ListView control in ASP.NET 3.5 combining it with LINQ and Lambda expressions and was finding myself duplicating a lot of code between ItemTemplate and AlternatingItemTemplate templates (I'll be posting the sample application that demonstrates using LINQ, Lambdas and Stored Procedures soon).  The AlternatingItemTemplate contained the same code as the ItemTemplate except for a CSS class added to the first <tr> element to change the background color.  Here's an example of both templates that were used initially:

It seemed like a waste (and a maintenance headache) to have both templates duplicating the same code while only needing background color changes so I decided to adjust it by adding a little code into the first <tr> tag to dynamically assign the CSS class based upon odd or even rows. It calls the ListViewDataItem class's DataItemIndex property and applies a modulus operation to it.  This isn't anything new of course (I've done the same type of thing for years with GridView and DataGrid controls), but it's a nice trick that can save a lot of duplicated code if you haven't seen it.

del.icio.us Tags: ,,

Videos You May Like

A Simple Introduction to Cisco CML2

0 3901 0

Mark Jacob, Cisco Instructor, presents an introduction to Cisco Modeling Labs 2.0 or CML2.0, an upgrade to Cisco’s VIRL Personal Edition. Mark demonstrates Terminal Emulator access to console, as well as console access from within the CML2.0 product. Hello, I’m Mark Jacob, a Cisco Instructor and Network Instructor at Interface Technical Training. I’ve been using … Continue reading A Simple Introduction to Cisco CML2

Creating Dynamic DNS in Network Environments

0 645 1

This content is from our CompTIA Network + Video Certification Training Course. Start training today! In this video, CompTIA Network + instructor Rick Trader teaches how to create Dynamic DNS zones in Network Environments. Video Transcription: Now that we’ve installed DNS, we’ve created our DNS zones, the next step is now, how do we produce those … Continue reading Creating Dynamic DNS in Network Environments

Cable Testers and How to Use them in Network Environments

0 731 1

This content is from our CompTIA Network + Video Certification Training Course. Start training today! In this video, CompTIA Network + instructor Rick Trader demonstrates how to use cable testers in network environments. Let’s look at some tools that we can use to test our different cables in our environment. Cable Testers Properly Wired Connectivity … Continue reading Cable Testers and How to Use them in Network Environments

Write a Comment

Share your thoughts...

Please fill out the comment form below to post a reply.