Saturday, June 19, 2010

PageMethods vs UpdatePanels

While sitting in just another meeting with another lead developer, I was told that they are using UpdatePanels with jQuery just because they read it somewhere over the internet.

I threw them with PageMethods alternate solution; (although it can be script'ize using ScriptManager.RegisterClientScriptBlock), especially when used with jQuery. To my bewilderment she did not know about the PageMethods; and I was gladly eager to tell her more! (0:

It is not "just another" way of interacting with server; its reliable, and communicates in the shortest forms possible - lowest client/server data exchange; plus, the response by the WebMethod gives the freedom to manipulate and interact with the HTML the way we want.

Its a static method that is exposed using WebMethod attribute; and is called using Javascript. Follow the following steps:

1. Set the `.EnablePageMethods` property of ScriptManager to True.


2. Mark the static method with WebMethod attribute
3. Call method using javascript. Like:

PageMethods.MyMethod(
 Arg1, 
 Arg2, 
 ArgN, 
 Incase_Success/*Optional*/, 
 Incase_Failure/*Optional*/);

function Incase_Success(results/*Returned by the method call*/ , userContext, methodName) { 
    alert(results); 
}

function Incase_Failure(errors, userContext, methodName) {
    alert(errors.get_Message()); 
}

The PageMethod calls the web method MyMethod with the defined number of arguments, and upon successful completion of the function call, it redirects the response to SuccessFunction or FailureFunction according the status that it has.

When calling the method, the first N parameters in the PageMethods call matches the parameters of the class method. In this example we have only only one parameter.

There are two optional parameters that takes the names of the methods to be called in case of failure or success. The callback for success, followed by the callback for failure.

Following is a quick comparison between UpdatePanel, and PageMethod from Encosia.

PageMethod data exchange,

An important point to note, is that most of us relate the the term web service with SOAP and XML. But in this case ASP.NET AJAX doesn’t use SOAP and XML.

Btw, this article is a MUST READ!

Happy coding!

1 comment:

  1. Hello,

    First of all let me tell you that your blog is pretty awesome and is cool to hear about how you make researching and share the results in your daily job.

    About your post,
    This is what I understand by reading your post:

    You can define some nice C# methods on your C# classes, and them call them by using a JavaScript simple solution by putting a ScriptManager AJAX control inside your WebForm, allowing JavaScript to statically call these methods.

    Is this right?
    You also mention that this stuff can be "script'ize", what do you mean? is this some sort of security concern?

    As far as I know, interacting between JavaScript and C# assemblies or whatever, implies having a .NET Handler so you can request stuff dynamically.

    Thanks!

    ReplyDelete

Related Posts

Popular Posts