Page methods: why I prefer them to be static...

Published Tue, Oct 24 2006 9:32

In my previous post, I've presented an alternative to using the old page instance methods that existed on previous releases of the CTP. Since I'm always late, i didn't realize that there was already a heated debate on the community and hadn't even noticed that Rick Stralh had a post where he complains about several things in the new release of the AJAX extensions. One of the things he wants back is instance page methods.

Though i really didn't enjoy the old instance page methods, i'll try to perform an honest and independent analysis on instance vs static page methods. If you're reading this, you need to know that i'm not affiliated with Microsoft in any way  nor have been in the past. So, I'm not really defending them, but only giving my opinion on this subject (instance vs static page methods).

Before going on, I should also mention that i didn't really use the old instance page methods and I think I won't be using the new static page methods either (though I really prefer the latter approach). Let's start by the speaking about the strenghts of the old instance page methods. In my opinion, there was one big obvious advantage: you could do all your work from the server side without having to write any Javascript code on the client to get the values you needed. For instance, if you had to get the values of 2 or 3 controls and then perform some calculations on it and return something back to the client, you could simply write your sever method with C# (or other .NET language) and annotate it with the WebMethodAttribute and you were ready to go.

This is a great, specially if you don't want to bother with the details associated with Javascript. Yes, you still need to call the method from the client (by using the global PageMethods object), but you no longer need to get the values of the controls in the client side and pass them as parameters to the server method since you'll be doing that (getting the values of the controls) on the server side, using the traditional server side approach (control.property).

For a long time, I used to think that instance page methods had another advantage: you could update the view state of the page from within an instance page method call. If this was true, it would be its greatest advantage since without it, you couldn't really update the view state without using an UpdatePanel and getting its HTML back (as you must recall, UpdatePanel always returned its HTML in previous releases of the platform, which isn't normally needed when you're using instance page methods).

Unfortunately, I was wrong about this. I've run a simple test with an ancient ATLAS project and I've confirmed that the view state isn't really updated during an instance page method. In other words, you can change the view state of the page on the server side, during an instance page method call, but that info isn't sent back to the client side. Due to that, the hidden view state field isn't updated and when you get the next partial/full postback or perform the next instance page method call, you'll have the same old values that existed before the instance page method call you've just made. As I said, I've just reached this conclusion by running an old ATLAS app and using fiddler to see what the page is getting back from the server (I really can't be sure if this was the behavior present on the previous July CTP, though I think it was).

The biggest disadvantage I see with page methods is that a method call results in sending all the existing form fields back to the server. For instance, let me give an example (which is really an extreme example of how things can be bad). Suppose you have 100 textboxes (sorry Garbin, you've influenced the number of controls:) ) and you need to get the values of 3 of them to perform some calculations on the server side. Using the old instance page method means that the page will be sending the values of the 100 textboxes + the other controls maitained inside the web form (for instance, suppose that you're using viewstate...yes, you'll get that field sent back to the server too). Remember: you just needed the values of 3 textboxes. Unfortunately, that doesn't matter when you use the old page methods since the client really need to send eveything back in order for you to use the controls during the remote instance page method call.

Since you can't really change the view state of the page, the only thing page methods are letting you do is making sure that you don't leave the confort of writing C# code on the server side where things are more coherent than in the client side. Now think about a performace point of view: are really page methods that great? shouldn't you be using Javascript and perform a simple web method call in order to get your result back? think about it...using the instance page method is easier and really confortable...but is it really performant? I'd say no and I'd even go further and say that most things (if not really all the things) that currently are being done with it could be done through web methods calls... that's why I didn't like the old instance page methods: you can build them easilly, but you're paying a huge price when you use them.

Now enter the new static page methods. If you've been using the old instance page methods, you already know that the new static methods are useless for you since you don't get any context associated with the method call (ie, you're no longer able to use the controls on the server side). Reusing the previous 100 TextBoxes example, you will need to get the values of the 3 Textboxes through Javascript in order to perform the remote page method call. I concede to the old instance page method users when they say that having a page method without context is almost useless.

But no totally useless, though, since they solve a problem which existed on the previous releases: how could you write a method on the server side and invoke it from the client without developing a web service? the new static page methods solve this problem and let you perform a remote method call without having to develop web services. The new static page method are also more performant since they don't automatically pack all the form fields when you perform a remote call (which is logical, since the server method is static after all).

So, the new static methods are useful if you're not into writing web services to perform remote server method calls.

Many say that not including the old instance page method calls means that AJAX extensions have less features than in previous releases. Well, that is a fact, but at least you've got one less thing to shoot yourself in the foot. On the other hand, I must say that having the old instance page methods really helped many to write remote method calls without using Javascript code and that there are lots of guys which really don't care about performance and just want to quickly build AJAX pages (not my view of how things should work, but everyone is entitled to think and act as they like).

So, in order to get everyone happy, I'm proposing some fixes for the next release. For starters, we should be able to call static and instance page methods from the client side (you already know that I won't be calling instance methods, but I'm sure that others will). Instance page methods would work as they did in previous releases and static methods would work as they do now. To support this, the team would have to include a new client class (like they did in previous releases) which would help gathering the data during the remote method call.

They'd probably had to add two global variables to the page: one would let you perform the static method calls and the other would let you perform the instance method calls. Nothing too fancy, I think... Allowing both kinds of calls would let everyone happy (including me :)) and i think that it might just be doable!

Filed under: ,

Comments

# Joel Rumerman said on Tuesday, October 24, 2006 11:07 PM

Luis,

Nice post.

One thing I'll add in favor of the instance based page methods is that with the .NET control hierarchy and the way that clientIds end up being concatenations of parent Ids and so forth, it can be difficult and a bit unruly to know the exact id of the DOM element you're trying to access when you're on the client. If I have a control that's three levels deep, MyPanel1_ctl0_ctl_0_1_MyLabel I have to emit the exact Client ID of MyLabel to the client in order to access it through JavaScript. This can be quite a pain in the ass. (Something like Script# could definitely help.) On the other hand, if I'm back on the server I can probably access the label's value through MyLabel or at least in a simpler way. The control tree is a *** to work with when you're solely working with it through JavaScript.

However, I definetly agree with your suggestion at the end of the post recommending that MSFT end the debate by supplying both static and instance based page methods. Let us make the decision, but don't take away our capabilities. Especially for us early adopters who may have relied upon them. Ahh-HEM! We don't need to be saved from ourselves.

(BTW, I'm writing this using FF2.0 and the inline spell checker rocks!)

# Luis Abreu said on Wednesday, October 25, 2006 3:25 AM

hello Joel.

well, i understand your point of view (that's why i proposed getting back page methods). I still think that page methods don't give us any additional value and I do think that we would be better without them.

the main problem i see with them is that they send a lot of data back to the server and you can only use that info as read-only info (which is was i still think that the proper thing to do in a ajax page is to use javascript to get the values from the dom).

i agree that sometimes it's hard to get the reference to the html element, but let's face it: you can easily inject global variables which point to the controls from the server side page (at least, that what i do and it really simplifies my client code)

# vikram said on Wednesday, October 25, 2006 7:30 AM

Hi Really nice post.

I wanted to know one more thing. NOW you do not get the Microsoft.Web.Ajax dll. This is installed in the GAC. but the hosting provider might not be good enough. What Can i do about that.

http://www.vikramlakhotia.com/New_Beta_version_of_the_ASPNET_AJAX_released.aspx

# Joel Rumerman said on Wednesday, October 25, 2006 10:59 AM

"you can easily inject global variables which point to the controls from the server side page (at least, that what i do and it really simplifies my client code)"

-- Something like "var myLabel = new Sys.UI.Label('myPanel_ctrl0_myLabel');", right being emitted through server code? That's normally what I've been doing too.

I guess I'm also a bit frustrated that MSFT used the Beta release to make this change. It's not so much an under-the-hood change as say switching from closures to prototypes, but a feature change. I guess I'm just not sure why they didn't make this a CTP change previously so it wouldn't feel like such a big change at the Beta release point.

# Luis Abreu said on Wednesday, October 25, 2006 3:00 PM

Hi guys.

vikram: though it may not be full proof, you can try to put the atlas dll in the bin folder and set the trust level of your web app to full trust. i haven't tested it yet.

Joel: i agree with you on the breaking change thing. however, i do really think that nobody should be using them due to the performance problems associated with them.

# LA.NET [EN] said on Wednesday, October 25, 2006 4:07 PM

It seems like Dino Esposito isn't happy with the changes made to the AJAX extensions . I certainly agree

# Tassos said on Tuesday, May 22, 2007 3:39 PM

Interesting...

# Pavlos said on Wednesday, May 23, 2007 12:42 AM

interesting

# Yannas said on Sunday, June 10, 2007 6:52 PM

Interesting...

# Anastassios said on Monday, June 11, 2007 7:00 PM

interesting

# Marios said on Thursday, June 14, 2007 4:16 AM

interesting

# Spyros said on Monday, June 18, 2007 5:21 AM

interesting

# Kosmas said on Tuesday, June 19, 2007 7:28 AM

Interesting...

# Panagiote said on Sunday, June 24, 2007 12:22 AM

interesting

# Constantine said on Sunday, June 24, 2007 9:18 AM

interesting

# Vassilis said on Monday, June 25, 2007 10:40 PM

Interesting...

# Maximos said on Saturday, July 28, 2007 4:32 AM

interesting

# Hermes said on Saturday, July 28, 2007 2:46 PM

interesting

# Vassilis said on Sunday, July 29, 2007 4:44 AM

interesting

# Angelos said on Sunday, July 29, 2007 9:53 PM

Interesting...

# Dimitrios said on Monday, July 30, 2007 1:55 PM

interesting

# Efstathios said on Tuesday, July 31, 2007 2:26 PM

interesting

# Milos said on Wednesday, August 01, 2007 7:12 PM

Interesting...

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above:  

Search

This Blog

Tags

Community

Archives

Syndication

Email Notifications

News




  • View Luis Abreu's profile on LinkedIn


    Follow me at Twitter

    My books

    Silverlight 4.0: Curso Completo

    ASP.NET 4.0: Curso Completo

    Portuguese LINQ book cover

    Portuguese ASP.NET 3.5 book cover

    Portuguese ASP.NET AJAX book cover

    Portuguese ASP.NET AJAX book cover