views:

395

answers:

3

What are the latest and greatest ways to move ViewState to bottom of the page

Can this be done in a IHttpHandler that can be specified in the web.config to intercept requests to "*.aspx"?

<httpHandlers>
    <add verb="*" path="*.aspx" type="MyApp.OptimizedPageHandler" />
<httpHandlers>

Other options is that this could be done in a IHttpModule, but that is not as performant, as it will intercept all requests.

Also it could be done in an a class deriving from the Page or MasterPage-class, but this is not as modular.

Are there any performance penalties to this?

+2  A: 

You can control how and where ViewState data is loaded and saved by creating a custom implementation of the PageStatePersister class. Then create a base class for all your ASPX pages and override the PageStatePersister method to return your custom implementation. This can then tap into whichever page events you want to store the viewstate per your requirements.

I question whether or not it's worthwhile though. Are you storing a ton of data int he ViewState unnecessarily? Maybe you can get more benefit easier by just using ViewState less or turning it off for some controls as opposed to just moving it to a different place within the HTML page.

Sam
A: 

Extending the Page class gives you the most control over page rendering. If you want to move ViewState to the bottom of a given page, use your custom base class for that page. If you don't need to, use the Page class.

The only performance risk when deriving from the Page class is in your implementation of overridden methods. Unless you're doing something particularly inefficient, there should not be any perceptible performance hit.

PunctuallyChallenged
A: 

After conducting some research I put together this blog-post.

I solved the issue by creating a HttpModule and applying a Response Filter, which modifies the output of the page and moves the ViewState to the bottom of the form.

public class ViewStateSeoHttpModule : IHttpModule {
    public void Init(HttpApplication context) {
        context.BeginRequest += new EventHandler(BeginRequest);
    }

    private void BeginRequest(object sender, EventArgs e) {
        HttpApplication application = sender as HttpApplication;

        bool isAspNetPageRequest = GetIsAspNetPageRequest(application);
        if(isAspNetPageRequest) {
            application.Context.Response.Filter =
                new ViewStateSeoFilter(application.Context.Response.Filter);
        }
    }

    private bool GetIsAspNetPageRequest(HttpApplication application) {
        bool isAspNetPageRequest = application.Context.Handler is System.Web.UI.Page;
        return isAspNetPageRequest;
    }
    // [...]
Seb Nilsson
that is pretty optimistic... what about extensionless urls or if you're using something else than aspx?No, you should instead look at whether your current handler is a System.Web.UI.Page or not.bool isAspNetPageRequest = application.Context.Handler is System.Web.UI.Page;
BurningIce
Very very good input @BurningIce
Seb Nilsson