Category Archives: Web Development

jQuery: How to Avoid Conflicting Versions

If you are planning to use jQuery on your web page and there is already another version being used, to prevent any conflict between the two here is what you can do:

var j = jQuery.noConflict();// Do something with jQuery
j( "div p" ).hide();

// Do something with another library's $()
$( "content" ).style.display = "none";

The code above creates a new alias instead of jQuery called j and while the other library is using the $.

How To: Serialize and Deserialize JSON with .NET Framework

Serializing an Object to JSON

To serialize an object to JSON you will need  to create a data contract, which is a class containing all the properties marked with attributes. To demonstrate this I will create a Person class and apply a DataContractAttribute and DataMemberAttribute.

internal class Person
	internal string name;

	internal int age;

Now we need to write some code to populate the Person class with some data and then use DataContractJsonSerializer to serialize the object to Json and just for you information DataContractJsonSerializer has been available since .NET Framework 3.5. Continue reading

Sorting GridView using IComparer and Reflection

Recently I just did a small job for a client online using IComparer interface to perform sorting on a GridView control.

First I need to create class called Person. This class is going to contain the following properties: FirstName, LastName, Age.

public class Person
    private string _firstName;
    private string _lastName;
    private int _age;

    public string FirstName
        get { return _firstName; }
        set { _firstName = value; }

    public string LastName
       get { return _lastName; }
       set { _lastName = value; }

    public int Age
       get { return _age; }
       set { _age = value; }

Now I need to create a class called PersonComparer that will inherit from the IComparer interface. This comparer class will be use with the Sort() method from a List collection.

 using System.Collections;
 using System.Collections.Generic;
 using System.Reflection;
 using System;

 public class PersonComparer:IComparer
     private string _sortBy;

     public string SortBy
         get { return _sortBy; }
         set { _sortBy = value; }

     public int Compare(Person x, Person y)
         int result = 0;
         string propName = string.Empty;
         string sortDirection = "ASC";

         if (_sortBy.Contains(" DESC"))
             propName = _sortBy.Replace(" DESC", string.Empty).Trim();
             sortDirection = "DESC";

         if (_sortBy.Contains(" ASC"))
             propName = _sortBy.Replace(" ASC", string.Empty).Trim();
             sortDirection = "ASC";

         // Get type of Person Object
         Type t = typeof(Person);

         // Find the property name from the sortExpress passed in SortBy property.
         PropertyInfo propInfo = t.GetProperty(propName);

         if (propInfo != null)
             // Perform comparison on property value.
             result = Comparer.DefaultInvariant.Compare(propInfo.GetValue(x, null), propInfo.GetValue(y, null));

             if (sortDirection.Equals("DESC"))
                 result = -result;
         return result;

Continue reading