Basic Sample - How To Keep ASP.NET ViewState On The Server
2008-01-16 12:16
597 查看
Ref: Article
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 SolutionThe 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:
And here is how it looks without:
相关文章推荐
- Unable to use SQL Server because ASP.NET version 2.0 Session State is not installed on the SQL serve
- How to Host ASP.Net Web API on IIS Server
- How to deploy the ASP.NET MVC 3 website into the IIS7 on Windows server 2008
- CodeSnip: How to Get Id of the Record Using ASP.NET and SQL Server 2000
- ASP.Net DebugError解决方案[转]:Unable to start debugging on the web server.Debugging failes because integrated Windows authentication is not enabled.
- How ASP.NET Web Pages are Processed on the Web Server
- How to enable an ASP.Net application to run on a SharePoint virtual server
- How to configure the Virtual Path of ASP.NET Development Server to be “Root”
- How to handle the concurrency problems on ASP.Net Database
- How to set the DefaultButton in a Page Based on ASP.NET Master Page
- How to Create Text Image on the fly with ASP.NET
- Cutting Edge The ASP.NET View State
- ASP.NET Development Server failed to start listening on port
- How to debug ASP.Net program without the privilege of administrator
- How to freeze a dynamic aspx page into a static html page (on the server).(转:http://codebetter.com/blogs/peter.van.ooijen/archiv
- ASP.NET development server failed to start listening on port (dynamic port) & Error Unable to connect to Visual Stutio's localhost web server
- how to use javascript to control the usercotrol in the asp.net
- How to install ASP.NET 1.1 with IIS7 on Vista and Windows 2008
- How to get all Errors from ASP.Net MVC modelState?
- Cutting Edge The ASP.NET View State