去掉.net页面中的<input type="hidden" name="__VIEWSTATE...

问:

.net 网站生成 页面后,会有 <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTU5NzgzMjY1MQ8WBh4KUGFnZUNvdW50cwLsAh4JUGFnZUluZGV4Zh4JSnVtcFBhZ2VzAu0CFgICAw9kFhYCBw8QDxYGHg5EYXRhVmFsdWVGaWVsZAUEY19pZB4NRGF0YVRleHRGaWVsZAUHY190aXRsZR4LXyFEYXRhQm91bmRnFgIeCG9uY2hhbmdlBShTaG93KHRoaXMub3B0aW9uc1tzZWxlY3RlZEluZGV4XS52YWx1ZSk7EBUjBuaAu+ermQbljJfkuqwG5aSp5rSlBuS4iua1twbph43luoYG5rWZ5rGfBuaxn+iLjwblub/kuJwG5bGx5LicBuWuieW+vQbmuZbljZcG5Zub5bedBuemj+W7ugbmsZ/opb8G5rKz5Y2XBua5luWMlwnpu5HpvpnmsZ8G5rKz5YyXBui+veWugQblkInmnpcG6LS15beeBuWxseilvwblub/opb8G5LqR5Y2XBumZleilvwblroHlpI8J5YaF6JKZ5Y+kBueUmOiCgwbpnZLmtbcG6KW/6JePBuaWsOeWhgbmtbfljZcG6aaZ5rivBua+s+mXqAblj7Dmub4VIwExATIBMwE0ATUBNgIxOAIxOQIyMAIyMQIyMgIyMwIyNAIyNQIyNgIyNwIyOAIyOQIzMAIzMQIzMgIzMwIzNAIzNQIzNgIzNwIzOAIzOQI0MAI0MQI0MgI0MwI0NAI0NQMzNTQUKwMjZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dkZAIJDxYCHglpbm5lcmh0bWwF2hwgPGRpdiBzdHlsZT0ndGV4dC1hbGlnbjpjZW50ZXI7d2lkdGg6MTAwcHg7IGZsb2F0OmxlZnQ7J2NsYXNzPSdqeDAxJz48YSBocmVmPSdqeHNzLTIuaHRtbCc+5YyX5LqsPC9hPjwvZGl2PiA8ZGl2IHN0eWxlPSd0ZXh0LWFsaWduOmNlbnRlcjt3aWR0aDoxMDBweDsgZmxvY

这些东西怎么去掉
我把页面viewstate 设置了false,我在这个页面上 放了个搜索,到其他页面,这就参数没法获取到

望高人指教

回答:

不用去掉
protected override object LoadPageStateFromPersistenceMedium()
{
return null;
}

protected override void SavePageStateToPersistenceMedium(object viewState) { }
重写这两个方法就可以

2楼:

C# code
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; //添加引用using System.IO; using System.Threading; namespace CCL { ///<summary>/// BasePage 的摘要说明 ///</summary>publicclass BasePage : System.Web.UI.Page { #region 解决ViewState过于庞大的问题//由于这里添加了目录,所以要建立App_Data/ViewState目录.protectedoverrideobject LoadPageStateFromPersistenceMedium() { string viewStateID = (string)((Pair)base.LoadPageStateFromPersistenceMedium()).Second; string stateStr = (string)Cache[viewStateID]; if (stateStr ==null) { string fn = Path.Combine(this.Request.PhysicalApplicationPath, @"App_Data/ViewState/"+ viewStateID); stateStr = File.ReadAllText(fn); } returnnew ObjectStateFormatter().Deserialize(stateStr); } protectedoverridevoid SavePageStateToPersistenceMedium(object state) { string value =new ObjectStateFormatter().Serialize(state); string viewStateID = (DateTime.Now.Ticks + (long)this.GetHashCode()).ToString(); //产生离散的id号码string fn = Path.Combine(this.Request.PhysicalApplicationPath, @"App_Data/ViewState/"+ viewStateID); //ThreadPool.QueueUserWorkItem(File.WriteAllText(fn, value)); File.WriteAllText(fn, value); Cache.Insert(viewStateID, value); base.SavePageStateToPersistenceMedium(viewStateID); } #endregion } }


一个朋友给我的

=============================================================

如果你有一个非常酷的页面,页面上很多东西自动地响应用户操作而展现丰富的变化,你的ViewState是很有可能达到200K的。

这里是我将ViewState持久化保持在服务器端的代码,这样ViewState不占用网络带宽,因此其存取只是服务器的磁盘读取时间。并且它很小,可以说是磁盘随便转一圈就能同时读取好多ViewState,因此可以说“不占时间”。为了再“不占磁盘时间”,我还使用了缓存。


一下这段代码可以放在页面中,或者页面的父类中:

C# code
protectedoverrideobject LoadPageStateFromPersistenceMedium() { var viewStateID = (string)((Pair)base.LoadPageStateFromPersistenceMedium()).Second; var stateStr = (string)Cache[viewStateID]; if (stateStr ==null) { var fn = Path.Combine(this.Request.PhysicalApplicationPath, @"App_Data/ViewState/"+ viewStateID); stateStr = File.ReadAllText(fn); } returnnew ObjectStateFormatter().Deserialize(stateStr); } protectedoverridevoid SavePageStateToPersistenceMedium(object state) { var value =new ObjectStateFormatter().Serialize(state); var viewStateID = (DateTime.Now.Ticks + (long)this.GetHashCode()).ToString(); //产生离散的id号码var fn = Path.Combine(this.Request.PhysicalApplicationPath, @"App_Data/ViewState/"+ viewStateID); ThreadPool.QueueUserWorkItem(obj => File.WriteAllText(fn, value)); Cache.Insert(viewStateID, value); base.SavePageStateToPersistenceMedium(viewStateID); }



不使用Session,因为它会“丢失”。ViewState保存在磁盘上,即使服务器重新启动,也不会丢失页面状态。

下面这段可以放在Global.asax中,也可以根本不管:

C# code
protectedvoid Application_Start(object sender, EventArgs e) { var dir =new DirectoryInfo(this.Server.MapPath("~/App_Data/ViewState/")); if (!dir.Exists) dir.Create(); else { var nt = DateTime.Now.AddHours(-1); dir.GetFiles().ForEach(f => { if (f.CreationTime < nt) f.Delete(); }); } }



这可以确保绝对稳定可靠地工作。以后请放心使用ViewState,把交互式页面提高水平才是最重要的,不要纠缠在“ViewState太大”上。实际上,由于页面设计不够酷,交互变化看上去不够丰富,ViewState实在是太小太小了。

如果你使用了它有效提高了复杂交互页面的效率,可以说一下提高了多少?!如果你觉得没用,也可以说一下在什么情况下没用。

http://topic.csdn.net/u/20080530/22/ef0d36a4-6bd4-4eba-9743-b56f080b2161.html

http://topic.csdn.net/u/20080918/15/9af44a56-f77e-49c6-bc39-0ad44ad17c5c.html

posted @ 2012-05-11 09:35  洗碗心得  阅读(560)  评论(0编辑  收藏  举报