?
Solved

returning data from ASP.NET Ajax pagemethod in JSON

Posted on 2010-01-12
6
Medium Priority
?
2,462 Views
Last Modified: 2013-11-08
In an Ajax-enabled, C#, ASP.NET 3.5 web application, I need to return a datatable (having 2 columns) from a pagemethod, which will be used by client-side Javascript. So I would like the datatable to be returned by the pagemethod to be in JSON.
I read that it's not easy to serialize a datatable to JSON.

First question - Do pagemethods return JSON-serializable data in JSON by default? E.g if a pagemethod returns a C# array, will we have to write code in that pagemethod to serialize it to JSON?

If the answer to the above question is yes, I would like to convert the data in my 2-column datatable to a C# object which won't require me to write code for JSON serialization. The data in this datatable is pretty simple. The first column and the second column have one-to-many relationship, which means a single value in the first column has multiple values in the second column.

So from the pagemethod, I can return data in a hashtable or a 2-dimensional array, instead of the actual datatable.

In what C# object should I return the data from the pagemethod, so that I don't have to write code for JSON serialization, if possible and so on the client side, I can easily do 'eval' to deserialize it back to a format where it can be easily used in Javascript?

Thank you.
0
Comment
Question by:engg
  • 4
  • 2
6 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 26303735
I believe that the default for ASP.NET web services is SOAP, but that doesn't apply to normal page methods.  If you are talking about a web application, than there is no "default" serialization.  I haven't tried using the JSON serializer with a DataTable.  When I serialize a DataTable, I usually just add it to a DataSet, which is easily serializable.  
0
 

Author Comment

by:engg
ID: 26304362
Thanks Bob.

I would like to know in what format pagemethods serialize the data they return.

If it's JSON, I would like to know which C# data structures don't need explicit JSON serialization code. I know C# datatable does, so I can just convert C# datatable data into one of those data structures (rather than adding it to a dataset).

or Do I have to write code for JSON serialization for any C# data structure?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1500 total points
ID: 26305504
OK, the caffeine is kicking in this morning, and I am starting to wake up *GRIN*.

Code-behind page methods, since they are Javascript, would use JSON serialization.  Reviewed a few articles, like Rick Strahl's to remind myself about the problem serializing a DataTable.

DataTable JSON Serialization in JSON.NET and JavaScriptSerializer
http://www.west-wind.com/Weblog/posts/471835.aspx

I would think that a list of objects that represent the data in the table, with simple data types, would be significantly easier to serialize with a JSON serializer, without code.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:engg
ID: 26305577
:) Thanks Bob.
Working on a pagemethod now.
0
 

Author Comment

by:engg
ID: 26328688
Got it working. I return a C# array from a pagemethod and a javascript function understands it as a javascript array. I didn't have to write any serialization code.
0
 

Author Closing Comment

by:engg
ID: 31676427
Found a solution myself. This comment helped.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question