Microsoft Most Valuable Professional

Chris Pietschmann

An MVP From Wisconsin



ASP.NET AJAX: Use "custom" objects as Extender Control Properties

I’ve been working with the ASP.NET AJAX Extensions a lot lately, and have been learning a few trick on how to get stuff done. One of the things I’ve learned is how to create a Generic TypeConverter for ASP.NET Ajax to use when serializing my server-side objects to JSON when passing them down to the client-side.

Introduction

When creating an ASP.NET Extender Control (using ASP.NET AJAX and the AJAX Toolkit) you can add custom properties to your Extender (using the ExtenderControlProperty attribute) that get passed down to the client. Well, this is really cool and all, but you are restricted to using the “standard” variable types (string, int, bool, etc.); unless of course you want to create your own custom TypeConverter to convert your custom objects to JSON.

How do I create and use a JSON TypeConverter?

and, do I need to create a separate one for each of my “custom” objects? First off, it’s pretty simple, and secondly, you can create “generic” TypeConverter that will work with virtually all your objects.

Step 1: Create a new Extender Control (http://ajax.asp.net/ajaxtoolkit/Walkthrough/CreatingNewExtender.aspx)

Step 2: Create out “generic” TypeConverter to use with our object. Yes, this is really all the code necessary to create the “generic” TypeConverter; all the serialization is actually done for us by the System.Web.Script.Serialization.JavaScriptSerializer object.

public class GenericTypeConverter : TypeConverter
{
    public override object ConvertTo(ITypeDescriptorContext context, 
       System.Globalization.CultureInfo culture,
        object value, Type destinationType) 
   {
        JavaScriptSerializer jss = new JavaScriptSerializer();
        string s = jss.Serialize(value);
        return s;
    }
}

Step 3: Create your “custom” object that we want serialized and passed down to the client, and set the TypeConverter attribute to point to our GenericTypeConverter. In this example I’m using a Person object to keep things simple.

[TypeConverter(typeof(GenericTypeConverter))]
public class Person{
    private string _FirstName;
    public string FirstName
    {
        get { return _FirstName; }
        set { _FirstName = value; }
    }
     private string _LastName;
    public string LastName
    {
        get { return _LastName; }
        set { _LastName = value; }
    }
}

Step 4: Add our Person property to the ExtenderControl

Step 5: Add the code that deserializes our object on the client. Add the following line of code to the “initialize” method of your ExtenderControl’s behavior javascript file:

[code:c#] 

if(this._myPersonValue!=null){
this._myPersonValue=Sys.Serialization.JavaScriptSerializer.deserialize(this._myPersonValue);
}

Step 6: Acces our Person object properties on the client

[code:js] 

alert("The full name of my person is:\n"    + this._myPersonValue.FirstName + " " + this._myPersonValue.LastName);

Conclusion

Even though this ends up being a very simple thing to do, it took me a lot of time (a few months ago) to figure out since there aren’t very many articles out there on how to do stuff with ASP.NET AJAX.

I also kept this article pretty simplistic, since I’m offering the complete solution for download and code always speaks for itself.

Download Sample Code: CustomJSONTypeConverter.zip

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:
Categories: asp.net | General
Posted by crpietschmann on Saturday, June 16, 2007 2:53 PM
Permalink | Comments (7) | Post RSSRSS comment feed

Related posts

Comments

tradecruz

Friday, September 07, 2007 4:55 AM

tradecruz

Does this post submit?

tradecruz

Friday, September 07, 2007 1:15 PM

tradecruz

Chris - how would you implement a deserialize method in the GenericTypeConverter? Override the ConvertFrom? I am wanting to deserialize the object back from the client on the server. In my example, I am using the ClientState property of the control extender as the string input.

Thanks.

tradecruz

Friday, September 07, 2007 5:05 PM

tradecruz

Chris - I was able to create a JSON deserializer for the GenericTypeConverter overriding the ConvertFrom method. Unfortunately, I am not having any luck posting my code. If you want, I can email it to you to include in your post.

Let me know. Thanks!

Chris Pietschmann

Monday, September 10, 2007 3:17 PM

Chris Pietschmann

Actually, the System.Web.Script.Serialization.JavaScriptSerializer that is part of ASP.NET AJAX can do the deserializing itself, so you don't really need to write your own ConvertFrom.

Just do this:
JavaScriptSerializer jss = new JavaScriptSerializer();
MyObject obj = jss.Deserialize<MyObject>(strSerializedObject);

tradecruz

Tuesday, September 11, 2007 12:57 PM

tradecruz

I did a post to expand on your work here - http://forums.asp.net/t/1156042.aspx

The code you mentioned above is what I have in the override ConvertFrom method.

Please let me know what you think.

Thanks.

Hugh Lynch

Thursday, September 13, 2007 9:52 PM

Hugh Lynch

I have a Type object (from MethodInfo) and would like to be able to use the javascriptSerializer to deserialize it, but I do not know how to invoke the Deserialize/T/ method with a Type variable. Is there a way? I'm fairly new to generics.

Marco Anastasi

Wednesday, October 31, 2007 7:11 PM

Marco Anastasi

I mentioned this article in a post in my blog about JSON serialization / deserialization here: geekswithblogs.net/.../...in_aspnetajax_part3.aspx

Comments are closed

About the author

I'm Chris Pietschmann, go to the About Me page to learn more about me.

Search

Sponsors

Web.Maps.VE - ASP.NET AJAX Virtual Earth Mapping Server Control

Recent comments

Disclaimer


This work is licensed under a Creative Commons Attribution 3.0 United States License, unless explicitly stated otherwise within the posted content.
© Copyright 2004 - 2008 Chris Pietschmann