?
Solved

returning data from ASP.NET Ajax pagemethod in JSON

Posted on 2010-01-12
6
Medium Priority
?
2,422 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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

777 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