I really love jQuery. It makes a huge difference in user interfaces and usability on any site. Not only that, but it's so simple to implement. I feel like I'm starting to really get a grasp on its potential.
One area that's been a little annoying to work with is the integration of jQuery AJAX calls and ASP .NET. Luckily, there's a good article I found that details the process so you can have a function in your code-behind that jQuery can call.
Using jQuery to directly call ASP.NET AJAX page methods
The one thing I would add to this is passing data to your code-behind. He doesn't seem to cover that in this article, so I'll just make a quick point about that. I was actually pretty surprised at the process: in the data section of the ajax call, you want to specify the parameter name, and then the value. In most languages you just pass the right datatype into the appropriate place, but in jQuery and ASP .NET it looks like you have to specify both for it to work properly. Here's an example:
ASP .NET
[System.Web.Services.WebMethod]
public static String ServerSideFunction(String myArgument)
{
//server-side code goes in here
}
jQuery
$.ajax({
type: 'POST',
url: 'index.aspx/ServerSideFunction',
data: "{'myArgument':'"+ argumentValue +"'}",
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function(msg){
//success code goes here
},
error: function(xhr, msg){
//failz code goes here
}
});
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.