Labels

Monday, August 31, 2009

Why do ASP.NET AJAX page methods have to be static?

Hi,

 

For using the Page Methods the methods has to be defined using ‘Static’.

Now whey Static??

 

Understanding what the Page class is, and why we have it

 

Contorting the stateless HTTP protocol to accommodate ASP.NET’s WebForm paradigm was a considerable task for the ASP.NET team to accomplish. On top of that, going from the inline execution model of ASP classic to the event-driven model of ASP.NET also required major changes.

As such, the ubiquitous WebForms Page class was created to solve these various problems. Take this snippet, for example:

 

public partial class _Default : System.Web.UI.Page 
{
  protected void Page_Load(object sender, EventArgs e) 
  { 
    // Hello World.
  }
}
 

One of the Page’s most powerful features: Persistence

 

The Page brings a lot to the table. However, to answer the question of why page methods must be static, we need to focus on persistence.

Consider this example:

<asp:Label runat="server" id="Label1" />
<asp:Button runat="server" id="Button1" OnClick="Button1_Click" />
protected void Button1_Click(object sender, EventArgs e)
{
  Label1.Text += DateTime.Now.ToString();
}
 

Each time the button is clicked, the label will have the current time and date appended to its current contents. Even this elementary example illustrates how the WebForms Page automatically provides persistence for us.

 

Understanding how the Page does this

 

To implement this encompassing layer of persistence, the ViewState was created.

Each time a page is rendered to the browser, the Page serializes the state of its controls and then includes that information in the returned HTML, via a hidden form field named __ViewState. When a postback occurs, this hidden field can then be de-serialized to create an instance of the Page in the same state that it was at the end of the last request.

 

Oversimplifying, you can envision this constructor being used to re-instantiate the Page, at the beginning of every postback:

 
Page _Default = new Page(Request["__ViewState"]);
 

This re-instantiation of the Page from the ViewState is what keeps the entire WebForms machine rolling, postback after postback.

 

What does it mean that a method is static?

 

The key difference to understand is that a static method can be called without setting up a proper instance of the class it belongs to.

In a sense, it is a stateless method.

 

If you’re implementing page methods, you’re probably well aware of their excellent performance. They are especially performant compared to the UpdatePanel’s partial postbacks.

They are efficient largely because they do not require the ViewState to be POSTed and they do not create an instance of the Page, while partial postbacks do both of these things. As we now know, a page method couldn’t create an instance of the page even if desired, since the ViewState isn’t provided in the request.

 

This is exactly why they must be marked as static. They cannot interact with the instance properties and methods of your Page class, because a page method call creates no instance of the Page or any of its controls. Page methods are roughly equivalent to shorthand for standalone web services. In fact, the ScriptManager even calls them exactly as it would a regular web service.

 

Reference: LInk

 

Thanks & Regards,

Arun Manglick || Senior Tech Lead

 

 

1 comment:

  1. I feel there could be one more important & valid reason - as below.

    The PageMethods are called from the client-side without creating the Page class instance . Reason being - As such on the client side there is no provision to first instantiate the Page class and then call it's methods.

    This forces to declare them as 'Static'. I hope this is now explanatory why it is necessary to mark them as 'Static'.

    I believe this is same as marking the main method as 'Static' in C++ / Java -

    public static void main(string[] args)
    {
    // Code
    }

    Regards,
    Arun Manglick

    ReplyDelete