If you might ask, what would I want to use jQuery instead of ASP.NET AJAX (which is heavily integrated into WCSF, especially with its new bundle release), the answer would be “bandwidth” a.k.a performance.
While ASP.NET AJAX handles most asynchronous callback and partial-update rendering, it comes with a trade-off; you couldn’t control the data format exchanged between the server and the client. If you monitors the HTTP request made by ASP.NET AJAX UpdatePanel (using Fiddler or FireBug), you would see that even tiny update would require UpdatePanel retrieve the whole HTML code of that particular panel.
Compare it to jQuery, that enables developer to specify what data gets exchanged, it is possible to exchange only the neccesary data (preferably in JSON format, since it can be used directly by client-script to update the view). For more detailed explanation, refer to Dave’s post.
If you refer to Dave’s post, he posted a solution on how to use ASP.NET AJAX and still be able to use JSON data format. However, I still feel that ASP.NET AJAX generates a lots of overhead. I decided to try to use jQuery instead. The problem is, there is no straight-forward way to integrate both, especially how to call page-methods from jQuery (without using ASP.NET AJAX). One solution is to create HttpHandler to handle all AJAX-call, or one HttpHandler for each AJAX-call. But somehow, I think that solution would lead to scaterred logic in both pages and HttpHandler code.
I came up with a solution that allows all the logic reside in the related page.
For example, the client-side wants to update a view using data obtained from AJAX-call.
$.getJSON("Default.aspx?type=ajax&operation=get_data", function(result) {
$("#result_panel).text(result);
}
);
If you notice, the jQuery’s getJSON parameters, first parameter is the URL, and the second one is the callback function. In the URL, I included some parameters regarding the AJAX-call. The type is set to ajax, which is to notify that this request is AJAX-call. The operation parameter is used to enable several AJAX-call within one page.
protected void Page_Load(object sender, EventArgs e)
{
if(!this.IsPostBack)
{
if(Request.QueryString["type"] != null && Request.QueryString["type"] == "ajax")
{
HandleAjaxCall();
}
}
}
private void HandleAjaxCall()
{
if(Request.QueryString["operation"] != null && Request.QueryString["operation"] == "get_data")
{
Response.Write("This is result from server");
}
Response.Flush();
Response.Close();
}
In the page’s code, you need to intercept the request processing before it reaches standard life-cycle processing code (Page_Load, Page_Render, etc). The code in Page_Load event is pretty straight forward. The important point here lies in the HandleAjaxCall method, where the method ends with Response.Flush() and Response.Close(). If these two method aren’t present, the data returned to the client-script, will include the whole page’s HTML code, therefore we need to force the Response stream to close. Having finished those part, returning data to client-script is pretty straight forward. Just use Response.Write() with any data that you want. In the example above, I used simple string as the return value, but it is also possible to return a whole strongly-typed object value using JSON-format. I know it might not be an ‘elegant’ solution, but at least, it worked for me If anyone have better ways to do this, please comment mine
11 Responses
SimoneB
October 3rd, 2007 at 1:10 pm
1This actually works, but there are far betters way of handling ajax calls with ASP.NET. Just to cite some, Ajax.Net Professional, Jayrock, ASP.NET callbacks. Let alone ASP.NET Ajax without UpdatePanels. You can call web services and calls made in this fashion are much lighter.
It works, but it’s not so elegant, plus you need to write ad-hoc code whenever you want to make such a call. If you didn’t try yet, I suggest you give a look at Jayrock.
We’re using it on DotNetKicks and it works like a charm.
Steven Harman
October 3rd, 2007 at 2:29 pm
2A solution I’d really be interested in is using jQuery to call and handle WebServices build with ASP.NET AJAX on the server. And by WebService I mean a ScriptService - which tells the service to send it’s response in JSON format.
Of course, one easy way to do that is to just use the ScriptManager and the auto-magically built client-side WebService proxies that it pulls down to the page, but in some cases that is too heavy. Plus, I think it might be a good proof-of-concept and/or academic exercise to show the power of jQuery (and/or some of the other JavaScript libraries out there).
admin
October 3rd, 2007 at 3:14 pm
3Thx for your comment. I’ll definitely look into those. I find Jayrock’s solution quite similar as writing HttpHandler for each async calls I have.
However, I’m interested on Jayrock and Ajax.NET. I’m not sure though whether or not those solutions can integrate well with jQuery. For instance, jQuery has ajaxStart and ajaxStop event, usually used to display progress bar or modal popup window while ajax-call is in place.
I used ASP.NET AJAX too, and I was impressed. However, my heart fell on jQuery for its dynamic ability in selecting DOM element, and its extensibility without causing lots of extra javascript code. What do you think about that?
Anyway, as soon as I have more time, I’ll definitely try them =)
danni
October 4th, 2007 at 6:00 am
4Have a look @ http://subsonicproject.com/view/subsonic-rest-and-jquery.aspx
Atif Aziz
October 6th, 2007 at 8:30 pm
5If you check out the DNKWidgets sample over at http://dotnetkicks.googlecode.com/svn/trunk/Widgets/Web/index.html, you’ll see in there a working example of how jQuery integrates with Jayrock (including use of ajaxStart and ajaxStop). You can also download the sample http://code.google.com/p/dotnetkicks/downloads/list/DNKWidgets-v0.1.3.zip and run it locally.
Atif Aziz
October 6th, 2007 at 8:31 pm
6Sorry, the link to the current zip should have been:
http://dotnetkicks.googlecode.com/files/DNKWidgets-0.1.3.zip
Atif Aziz
October 6th, 2007 at 8:33 pm
7And another correction…
The link to the index.html got corrupted in the comment (comma was part of the sentence and not the URL) so here is the corrected and stand-alone version:
http://dotnetkicks.googlecode.com/svn/trunk/Widgets/Web/index.html
Tiny Url Service
October 6th, 2007 at 9:34 pm
8Hi, I came across your blog posting after searching for tiny url service and your post on Integrating jQuery with ASP.NET makes an interesting read. Thanks for sharing. I will research more next Saturday when I have the day off.
Tiny Url Service
October 14th, 2007 at 9:34 pm
9Hello from Chicago! I came across your blog posting after searching for tiny url service and your post on rating jQuery with ASP.NET makes an interesting read. Thanks for sharing. I will research more next Sunday when I have the day off.
JH
November 14th, 2007 at 6:02 pm
10You can call an ASP.net webservice and from JQuery via the following. Note this would allow a JSON string to be returned:
j.ajax({
type:’GET’
,url:’//’
,data:{’WeekStart’:'11/11/2007′}
,success:function(r){alert(r.text)}
,error:function(r){alert(’Error=’+ r.text)}
,contentType:’application/json’
});
JH
November 14th, 2007 at 6:05 pm
11As an additional note to the above comment you would also need to decorate the web method with UseHTTPGet=true
RSS feed for comments on this post · TrackBack URI
Leave a reply
RSS Feed
Bookmark
Blogroll
Advertisement
Recent Posts
Recent Comments
Dream of a journey is proudly powered by WordPress - BloggingPro theme by: Design Disease