Alik Levin's

Clarity, Technology, and Solving Problems | PracticeThis.com 

January, 2008

  • Alik Levin's

    How To Keep ASP.NET ViewState On The Server – Revised

    • 2 Comments

    This is a follow up post to Basic Sample - How To Keep ASP.NET ViewState On The Server

    ASP.NET 2.0 offers much easier built-in mechanism to save ViewState on the server in Session using SessionPageStatePersister. Thanks to Russ who pointed me to this feature available in ASP.NET 2.0 via comments in the previous post.

    The details can be found here ASP.NET 2.0 Page State Persister [thanks go to Eddie from our own ACE team for the pointer].

    Short analysis. Using SessionPageStatePersister to store VIewState is very convenient as there is no code needed, almost. The down side is that this approach allows storing ViewState only in Session while the approach described in previous post allows routing ViewState to any storage of your choice – Session, Cache, DB, Web Services – up to your imagination. This can be important when considering scalability aspects of the solution that kazlak pointed out in the comments for the previous post.

    To commentators. Big-thank-you for leaving your insightful comments. Sharing your insights helps me to improve and I am sure it also enriches this blog readers experience – THANKS! Please comment more and leave your web sites/blogs so I can link back to you and give you full credit to your insights.

  • Alik Levin's

    Basic Sample - How To Keep ASP.NET ViewState On The Server

    • 10 Comments

    During recent few engagements with my customers I've noticed  VIewState is extensively [unintentionally] used. ViewState is on by default. The result is heavy weight Html that round trips on the network. This causes slow response time and high network utilization that affects another applications using the same network.

    How to remove ViewState from the network completely while taking an advantage if its functionality at same time?

    This post walks through basic steps of creating ASP.NET Base Page that implements functionality allowing saving ViewState on the server. The approach reduces dramatically network utilization.

    Summary of steps

    • Step 1 - Create Visual Studio Solution. 
    • Step 2 - Implement Base Page
    • Step 3 - Inherit Each ASPX page from Custom Base Page
    • Step 4 - Test The Solution

    The following section describes each step in details.

     

  • Step 1 - Create Visual Studio Solution.  Open Visual Studio 2008 and create empty solution, found under "Visual Studio Solutions". Name it BasePageSample. In Solution explorer right click the solution and add new project. Choose "ASP.NET Web Application" under Web node in "Add new Project" dialog. Name it SampleWebToUseExternalBasePage. Right click the solution in Solution Explorer and add new project. Choose "Class Library" under Windows node. Name it MyBasePage. In MyBasePage project add reference to System.Web assembly. Right click MyBasePage project and add new class, name it LeaveViewStateOnTheServer.cs.

     

  • Step 2 - Implement Base Page.  While in LeaveViewStateOnTheServer class add using declarations and inherit from Page type:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Web.UI;
    using System.Web;
    namespace MyBasePage
    {
        class LeaveViewStateOnTheServer : Page
        {
        }
    }
    Override two relevant methods that handle Loading and Saving of ViewState:
    protected override object LoadPageStateFromPersistenceMedium()
    {
        object viewStateBag;
        string m_viewState = (string)Session["ViewState"];
        LosFormatter m_formatter = new LosFormatter();
        try
        {
            viewStateBag = m_formatter.Deserialize(m_viewState);
        }
        catch
        {
            throw new HttpException("The View State is invalid.");
        }
        return viewStateBag;
    }
    protected override void SavePageStateToPersistenceMedium(object viewState)
    {
        MemoryStream ms = new MemoryStream();
        LosFormatter m_formatter = new LosFormatter();
        m_formatter.Serialize(ms, viewState);
        ms.Position = 0;
        StreamReader sr = new StreamReader(ms);
        string viewStateString = sr.ReadToEnd();
        Session["ViewState"] = viewStateString;
        ms.Close();
        return;
    }
    This is a basic code that can be adopted and adapted to specific needs. The code mostly based on Dino Esposito's code.

  • Step 3 - Inherit Each ASPX page from Custom Base Page. Right click SampleWebToUseExternalBasePage project and reference to MyBasePage project. Right click SampleWebToUseExternalBasePage project and add new ASPX page, name it default.aspx. Open default.aspx.cs code behind and inherit the page from custom Base Page:
    using MyBasePage;
    
    namespace SampleWebToUseExternalBasePage
    {
        public partial class _Default : LeaveViewStateOnTheServer

  • Step 4 - Test The Solution. Add any control to the page from the toolbox. GridView uses ViewState the most. Here is how rendered Html For GridView looks with default ViewState behavior:

    image

    And here is how it looks without:

    image 

     

    My Related Posts

    Related Resources

    Download Sample Visual Studio 2008 solution from my SkyDrive:

  • Alik Levin's

    Consume patterns&practices Guidance Explorer Via RSS Using Outlook 2007

    • 6 Comments

    patterns&practices recently released new version of Guidance Explorer [GE]. One of the most important addition was enabling RSS on the online GE store. What does that mean? It means you can consume distilled security, performance, and Visual Studio wisdom in any RSS reader of your choice. It means you can leverage familiar environment to consume close to 4000 technical gold nuggets.

    This post describes how to set up RSS feed inside Outlook 2007 to read GE’s content.

    Summary of steps

    • Step 1 – Download latest GE bits
    • Step 2 – Create designated offline PST file
    • Step 3 – Subscribe to GE’s RSS feed in Outlook 2007
    • Step 4 – Test drive GE’s content using Outlook 2007

    Next section describes each step in detail

    • Step 1 – Download latest GE bits. Navigate to GE’s releases page and review latest release notes. Also review GE’s FAQ’s. Download latest release – it appears in upper right corner of the page. Extract downloaded zip file and navigate to bin folder. Double click GuidanceExplorer.exe file. GE appears on your screen.
    • Step 2 – Create designated offline PST file. Switch to Outlook 2007. Open “Data File Management…” found in “File” menu. Click “Add…” in “Data Files” tab to create new PST file. Name it GE [or whatever you like]. Saving GE’s items in dedicated offline PST file will keep your Outlook’s online store from overflowing and it is easier to back up too.
    • Step 3 – Subscribe to GE’s RSS feed in Outlook 2007. Switch to GE. Right click on “patterns & practices Library” node in GE and choose subscribe to RSS feed:
      image
      Internet Explorer will open and will try to display the feed. Do not get scared by the message telling you that IE cannot show the feed as follows:
      image
      Just grab the URL from the address bar – highlight it and copy it to clipboard [ctrl+C]. Switch to Outlook 2007. Right click “RSS Subscriptions” node and choose “Add a New RSS Feed” as depicted below:
      image
      Paste [ctrl+V] the URL from the clip board into the dialog and hit OK. Hit “Advanced…” button in the next dialog box and configure the feed to sore the items inside the newly created PST file:
      image
    • Step 4 – Test drive GE’s content using Outlook 2007. After Outlook 2007 finishes downloading the items you can test drive familiar functionality such as instant searching or forwarding the items as an emails.

    Heaven.

    My related posts

    Related materials

  • Page 1 of 4 (10 items) 1234