Server Side ViewState 在服务器端存贮ViewState (ASP.NET 2.0)

 View State 在 Asp.Net 的 postback 机制中有着举足轻重的地位,但是在带宽紧张的环境下(比如跨国访问)庞大的hiddenfield 往往成为系统瓶颈。有什么办法可以在服务器端保存View State吗?答案是肯定的。

方法一:
The Code Project 上的一篇文章Keep ASP.NET ViewState out of ASPX Page for Performance Improvement ,采用override Page 的SavePageStateToPersistenceMediumLoadPageStateFromPersistenceMedium 两个方法,把View State存储在Session或cache中。
但是这种方法总显的过于繁琐,有没有什么更简便的方法呢?


方法二:
ASP.NET 2.0 Quick Start中有这么一段

    Page-state, (the cumulative view-state and control-state for the controls and the page) is typically persisted to the page's response, using the HiddenFieldStatePersister (it is the VIEWSTATE hidden field). You can override the PageStatePersister on the Page for custom persistence however. Given the adaptive nature of controls to the requesting device, some devices may not handle significant amounts of data that are typically 'round-tripped'. Through the PageAdapter configured for a device, the state can be persisted to alternative sources. ASP.NET 2.0 defines two page state persisters, HiddenFieldPageStatePersister and SessionPageStatePersister.


其实ASP.NET 2.0中已经内置了把ViewState存贮在Session中的SessionPageStatePersister,只要override Page的PageStatePersister属性,把返回HiddenFieldPageStatePersister 改为返回SessionPageStatePersister就可以了。这样实在是很方便哦。

但是我又发现了一件很不爽的事情,我们来看一下Page.PageStatePersister的代码:

protected   virtual  PageStatePersister PageStatePersister
{
      
get
      {
            
if  ( this ._persister  ==   null )
            {
                  PageAdapter adapter1 
=   this .PageAdapter;
                  
if  (adapter1  !=   null )
                  {
                        
this ._persister  =  adapter1.GetStatePersister();
                  }
                  
if  ( this ._persister  ==   null )
                  {
                        
this ._persister  =   new  HiddenFieldPageStatePersister( this );
                  }
            }
            
return   this ._persister;
      }
}

我们虽然可以重载PageStatePersister属性,却不能修改_persister。在Page的其他很多地方是直接调用_persister的。这样可能会造成某种程度的不一致。
有什么办法呢?注意到上面的那个PageAdapter吗?我们就拿它开刀。


方法三:
首先写一个我们自己的PageAdapter

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Web.UI;

namespace  my
{
    
public   class  PageStateAdapter : System.Web.UI.Adapters.PageAdapter
    {
        
public   override  PageStatePersister GetStatePersister()
        {
            
return   new  SessionPageStatePersister( this .Page);
        }
    }
}


然后在站点下添加App_Browsers目录,并在目录下添加PageStateAdapter.browser

< browsers >
    
< browser  refID ="Default" >
        
< controlAdapters >
           
< adapter  controlType ="System.Web.UI.Page"  adapterType ="my.PageStateAdapter"   />
        
</ controlAdapters >
    
</ browser >
</ browsers >


好了我们的Server Side View State完成了,你可以比较一下前后生成的文件大小,越是复杂的页面效果越明显。当然这是以牺牲服务器性能为代价的。需要注意的是:所谓的server side,还要依赖于站点的session的存贮方式是如何配置的,不过这不在本文的讨论范围之内。

 

注意:下列服务器控件不能禁止ViewState
Textbox
Checkbox
Checkbox List
RadioButtonList
上面控件的状态通过IPostBackEventHandler 和 IPostBackDataHandler接口处理,而不是ViewState的机制,所以EnableViewState没有效果。

最干净的方法是去掉<form runat=server> 表单
如果你的程序允许的话,
<input type="hidden" name="__VIEWSTATE" value="...">
会消失的无影无踪

要查看页面中控件的ViewState使用情况可以在ASP.NET页面Trace中查看
http://www.zzchn.com/edu/20080727/90699.shtml

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET ViewState 是一种用于在 Web 应用程序中跨请求存储数据的机制。以下是一个使用 ViewState 的示例: 假设您有一个页面,其中包含一个文本框和一个按钮。用户在文本框中输入一些文本,然后单击按钮。在单击按钮时,将在服务器端处理程序中使用 ViewState 存储文本框中的值,并在页面上显示它。 以下是一个简单的 ASP.NET 页面代码示例,它演示了如何使用 ViewState 存储和检索文本框中的值: ```html <%@ Page Language="C#" %> <!DOCTYPE html> <html> <head runat="server"> <title>ViewState Example</title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br /> <asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" /> <br /> <asp:Label ID="Label1" runat="server"></asp:Label> </div> </form> </body> </html> ``` 在按钮单击事件处理程序中,我们将文本框中的值存储在 ViewState 中,并将其显示在页面上: ```csharp protected void Button1_Click(object sender, EventArgs e) { string text = TextBox1.Text; ViewState["myText"] = text; Label1.Text = "Text saved: " + text; } ``` 在页面加载事件处理程序中,我们检索存储在 ViewState 中的值,并将其显示在页面上: ```csharp protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (ViewState["myText"] != null) { string text = (string)ViewState["myText"]; Label1.Text = "Text retrieved: " + text; } } } ``` 通过这种方式,我们可以在页面上保留用户在文本框中输入的值,即使用户单击其他按钮或导航到其他页面。请注意,ViewState 可能会增加页面大小,并增加网络传输时间。因此,我们应该谨慎使用 ViewState,并仅在必要时使用它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值