Tagged: tips

Which JSON Serializer to use?

This is an easy one, but just want to make a note for easy reference. JSON is more ubiquitous than ever these days in ASP.Net web development. From AJAX enables web sites to MVC AJAX, JSON is “the” data-interchange format we want to use.

There are 2 possible class libraries that you can use in ASP.net to serialize objects to JSON and back. If you are using WCF and your custom objects are marked as [Data Contratcs], then DataContractJsonSerializer (System.Runtime.Serialization.Json) is the class you want to use. But if you are using an object that you cannot mark as a DataContract (may be part of a third-party component), then you want to use JavaScriptSerializer (System.Web.Script.Serialization).

Here is some code snippets that shows both approaches:

/* Using JavaScriptSerializer */
String s;
JavaScriptSerializer ser = new JavaScriptSerializer();
ser.MaxJsonLength = Int32.MaxValue;
s = ser.Serialize(obj);

JavaScriptSerializer ser = new JavaScriptSerializer();
ser.MaxJsonLength = Int32.MaxValue;
obj = ser.Deserialize(json);
return (obj);

/* Using DataContractJsonSerializer */

DataContractJsonSerializer ser = new DataContractJsonSerializer(obj.GetType());
MemoryStream ms = new MemoryStream();
ser.WriteObject(ms, obj);
string retVal = Encoding.Default.GetString(ms.ToArray());

T obj = Activator.CreateInstance();
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
DataContractJsonSerializer ser = new DataContractJsonSerializer(obj.GetType());
obj = (T)ser.ReadObject(ms);

Happy programming!

A LINQ Thought

LINQ stands for Language Integrated Query. I want to stress on “Integrated” because I think the strength of LINQ comes from the fact that it is a data query engine that is integrated with .Net languages. This very reason gives us many features that we as developers are comfortable
using in a language into our data manipulation operations.

Lets take a simple example,

var query
= from customer in CustomerTable.AsEnumerable()
where customer.Field("Name").ToLower().Contains("John")
select customer;

In the above example, we are using simple string functions that are part of C# string class to do our comparisons for our data fetch.
Lets take another example, where we will use a C# function to evaluate a sub-query/expression,

var query
= from customer in CustomerTable.AsEnumerable()
select new Customer 
Name = customer.Field("Name"),
State = customer.Field("State"),
CustomerType = GetCustomerType(customer.Field("State"))

In the above example, GetCustomerType() function is used inline with a LINQ query to evaluate some business rule.
In short, with great flexibility like this, LINQ has made life of developers much easier!


Using Popup Extender – Some tips

Popup extender is a very useful control included in AJAX Control tool kit. You can find
many articles and blogs if you search internet for this. You can download toolkit from here.
and you can see controls in action live from here.

Here is some of my observations:

When you use Popup Extender, if there is any dropdown list controls in your parent page, when popup is displayes, extender will automatically make the dropdowns invisible. This is to prevent dropdown to show up over the popup (since popup is rendered as a “div”). But many times I have noticed that, extender fails to set the visibility to hidden when popup is displayed. In these instances, we should find these controls and make these style changes ourselves. Another funky thing I have noticed that I have noticed is once popup unloads, the dropdowns still does not show up! Again, workaround is to get handle to these controls and make it visible again on popup unload.

If you have a master page, your page controls are rendered (including popup extender) with control prefixes for Id/names. So it is better to use $find with BehaviorId of your popup extender and finding control using that instead of using a $get with id of the control.
Another weird issue, I have seen with popup extender is that, some of my controls dissappear on popup was shown the second time. After a lot of digging and trial and error, I changed the markup to add table to hold the controls…and it started working !!!

    <ajaxToolKit:ModalPopupExtender     TargetControlID="myButton"
    ID="myPopUp" runat="server" PopupControlID="popUpPanel"
    BackgroundCssClass="modalBackground" BehaviorID="myPopUpBehavior"
//to show popup
var modal = $find('myPopUpBehavior');
if (modal!=null) modal.show(); 
//to hide popup
var modal = $find('myPopUpBehavior');
if (modal!=null) modal.hide(); 

Popup Extender requires you to provide a TargetControlID, so if you want to have a popup extender for a control that is dynamically created, you can attach the popup extender to a hidden form field design time, so that your code compiles, and in run time attach to your dynamically created control.