Improve ASP.Net Website performance using PageAdapter

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

Generally we are binding code with Gridview like this:

Gridview1.Datasource = dataset;

After that our page loads data and it looks ok, but when we look into view source of the page.
We see some thing 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 appear , select "Browser File" from given list.

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

  <browser refID="Default">
      <adapter controlType="System.Web.UI.Page" adapterType="PageAdapterServices"/>

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

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

Popular posts from this blog

Calling REST APi from server side code - C#

All about SharePoint 2010 Content Type Hub

Calling ASP.Net WebMethod using jQuery AJAX