Improve ASP.Net Website performance using PageAdapter

If you are working on a website then there might be a chance to handle a large amount of data on a public site.

Generally, we are binding code with Gridview like this:

Gridview1.Datasource = dataset;
GridView.DataBind();

After that our page loads data and it looks ok, but when we look into view source of the page.
We see something like this.

And this content is going to load every time a user refresh/reload this page.
In ASP.Net 2.0, PageAdapter class was introduced which removes this view state source from source code of a webpage.
To implement this lets start by creating a webpage (.aspx)
Step 1: Write your code to bind data with Gridview on page load.
Step 2: Add a class file in App_Code folder, name this class file as “PageAdapterServices”
Code in this class should look like this.
using System;
using System.Web.UI.Adapters;
using System.Web.UI;

 public class PageAdapterServices: PageAdapter
{
    public override PageStatePersister GetStatePersister()
    {
        //return base.GetStatePersister();
        return new SessionPageStatePersister(this.Page);
    }
}

Step 3: Now Add ASP.Net folder “App_Browsers” in your solution. Right click on this folder and click on Add new Item. Once the installed template dialog box appears, select “Browser File” from given list.

Step 4: Now replace below code in .browser file that you added in the last step

<browsers>
  <browser refID=”Default”>
    <controlAdapters>
      <adapter controlType=”System.Web.UI.Page” adapterType=”PageAdapterServices“/>
    </controlAdapters>
  </browser>
</browsers>

Note : adapterType=”PageAdapterServices” here PageAdapterServices is the name of class.

Now run your page and see view source, you will find the difference.
Enjoy 🙂

1 thought on “Improve ASP.Net Website performance using PageAdapter”

Leave a Comment

RSS
YouTube
YouTube
Instagram