Click here to Skip to main content
13,404,818 members (37,591 online)
Click here to Skip to main content
Add your own
alternative version


31 bookmarked
Posted 12 Oct 2003

Comparison Table in ASP.NET using Attributes and Reflection.

Rate this:
Please Sign up or sign in to vote.
A generic scheme to compare the properties of multiple instances of a class in ASP.NET using Attributes and Reflection.


Most of the online stores support a feature where the user can select multiple products from different vendors and compare them. For example, in, user can select different models available in digital cameras and do a comparison between them. This comparison table basically lists the key features that the user will be looking for in a digital camera and displays each model's capability. An example of such a comparison table is shown below:

Comparison Table in

Recently I was reading the requirements given by one of our business folks and I realized that it was similar to the comparison table displayed in these websites. Since the development platform is .NET, I decided to exploit the features of .NET.

Problem Definition

  • User must be able to select different objects and do a comparison of their properties. Objects compared will be always of the same type.
  • Must be able to reuse this implementation in other existing and new web applications.


  • Generic scheme so that this can be reused in multiple projects.
  • Scheme should be simple, easy to understand, extensible and customizable.

Features used

After thinking for a while, I decided to use the following features of .NET to meet the objectives.

  • Attributes
  • Reflection
  • Web Custom Control (ASP.NET)

How the scheme works

I defined an attribute called CompareAttribute which has two properties namely DisplayTitle and DisplayOrder. Any class whose properties have to be listed in the comparison table should be decorated with this attribute.

public class CompareAttribute : System.Attribute
    private int __displayOrder = 0;
    private string __displayName = string.Empty;

     public CompareAttribute(int DisplayOrder, string DisplayName)
        __displayOrder = DisplayOrder;
        __displayName = (DisplayName == null) ? "<undefined>" :

    public int DisplayOrder
            return __displayOrder;

    public string DisplayName
            return __displayName;

I defined a web custom control called DiffTable which contains the logic to render the comparison table. This custom control exposes a property called ObjectsToCompare of type ArrayList. If you provide an arraylist with the list of objects that you want to compare, then the control will do the necessary magic and displays the Comparison table.

To illustrate this scheme, I have defined a sample class called Camera. This camera class has five properties - Name, Model, Price, Warranty and HasCarlZeiss. I have decorated each property with the compare attribute.

Class properties decorated with the compare attribute will look like:

[Compare(5, "Warranty Info")]
    public string Warranty
            return __warranty;
            __warranty = (value == null) ? string.Empty : value.Trim();

The custom control uses reflection to identify the type of objects being compared. Also it make sure that the user has assigned at least 2 object instances for comparison.

private SortedList FetchComparableProperties(Type type)
        SortedList prps = new SortedList(32);
        CompareAttribute cmp = null;

        foreach(PropertyInfo prpInfo in type.GetProperties())
            cmp = (CompareAttribute)Attribute.GetCustomAttribute(
                 prpInfo, typeof(CompareAttribute));
            if(cmp != null)
                    throw new Exception(string.Format(
                       "A property with the same display" +
                       " order of {0} already exists.",
                prps.Add(cmp.DisplayOrder, prpInfo);

        return prps;

The above code inside the custom control shows the logic used to identify the properties marked with CompareAttribute and arrange the properties in the sorted order based on the Display Order value specified in the compare attribute decoration.

The download contains a sample web application which displays the comparison table generated by this scheme. The download also contains the source code of the web custom control. I have added enough documentation to each functions within the web custom control for easy understanding.

Sample Comparison Table Output


  • Since this scheme is based on metadata, even if new classes are added to the solution in the future, just by decorating the properties with the compare attribute will enable instances of this class to be compared without any change to the existing code.
  • Since the core logic is implemented as a web custom control, it is self contained and easily distributable. This control can be reused in multiple web applications.


  1. This scheme can be extended to support comparison with grouping - related properties will be grouped under a heading (similar to the feature in
  2. Image can be shown in the comparison table for each objects being compared by adding properties like ImageUrl.
  3. And more...


To implement functionality similar to this in classic ASP, it will be very tedious and more time consuming. With the help of .NET framework, things have become very easy and achievable in short time. I assume that everyone will appreciate the fact that .NET is making our life much easier.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Karthikeyan Arunachalam
Web Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

News Pin 6:28
suss 6:28 
Questioncompare two string Pin
syed riyazkhan24-Oct-07 1:24
membersyed riyazkhan24-Oct-07 1:24 
Generalimprovement Pin
thany19-Jun-07 2:26
memberthany19-Jun-07 2:26 
Generalabout image Pin
Anonymous2-Dec-04 2:53
sussAnonymous2-Dec-04 2:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180221.1 | Last Updated 13 Oct 2003
Article Copyright 2003 by Karthikeyan Arunachalam
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid