Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET: Moving ViewState to bottom of page

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?

like image 245
Seb Nilsson Avatar asked Mar 04 '10 14:03

Seb Nilsson


2 Answers

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.

like image 144
Samuel Neff Avatar answered Sep 28 '22 00:09

Samuel Neff


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;
    }
    // [...]
like image 21
Seb Nilsson Avatar answered Sep 28 '22 01:09

Seb Nilsson