时间:2021-05-28
复制代码 代码如下:
public class XVPage : Page
{
static private DirectoryInfo _Dir;
private DirectoryInfo Dir
{
get
{
if (_Dir == null)
{
_Dir = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data"));
if (!_Dir.Exists)
_Dir.Create();
_Dir = new DirectoryInfo(Path.Combine(_Dir.FullName, "ViewState"));
if (!_Dir.Exists)
_Dir.Create();
}
return _Dir;
}
}
protected override object LoadPageStateFromPersistenceMedium()
{
PageStatePersister ps = this.PageStatePersister;
ps.Load();
if (ps.ControlState != null)
ps.ControlState = AntiSerialization((string)ps.ControlState);
if (ps.ViewState != null)
ps.ViewState = AntiSerialization((string)ps.ViewState);
return new Pair(ps.ControlState, ps.ViewState);
}
protected override void SavePageStateToPersistenceMedium(object state)
{
PageStatePersister ps = this.PageStatePersister;
if (state is Pair)
{
Pair pair = (Pair)state;
ps.ControlState = pair.First;
ps.ViewState = pair.Second;
}
else
{
ps.ViewState = state;
}
if (ps.ControlState != null)
ps.ControlState = AntiSerialization(ps.ControlState);
if (ps.ViewState != null)
ps.ViewState = AntiSerialization(ps.ViewState);
ps.Save();
}
private object AntiSerialization(string stateID)
{
string stateStr = (string)Cache[stateID];
string file = Path.Combine(Dir.FullName, stateID);
if (stateStr == null)
stateStr = File.ReadAllText(file);
else
Cache.Remove(stateID);
return new ObjectStateFormatter().Deserialize(stateStr);
}
private string AntiSerialization(object obj)
{
string value = new ObjectStateFormatter().Serialize(obj);
string stateID = (DateTime.Now.Ticks + (long)value.GetHashCode()).ToString(); //产生离散的id号码
File.WriteAllText(Path.Combine(Dir.FullName, stateID), value);
Cache.Insert(stateID, value);
return stateID;
}
protected override void OnUnload(EventArgs e)
{
base.OnUnload(e);
DateTime dt = DateTime.Now.AddMinutes(-20);
foreach (FileInfo fl in Dir.GetFiles())
if (fl.LastAccessTime < dt)
try
{
fl.Delete();
}
catch
{
}
}
}
只需要在页面后台中继承XVPage 就可以了
public partial class Index_Content : XVPage
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
刚刚开始学习ASP.NET,对其中的很多内容不是很了解,这篇文章很好地向我解释了ASP.NET中常用状态的含义,把它放在这里备用:__VIEWSTATE:页面状
ASP.NET中的HTML服务器控件是服务器可理解的HTML标签。ASP.NET文件中的HTML元素默认作为文本进行处理。为了使这些元素可编程化,需要向HTML
在ASP.NET包含文件的方法有:1.2.3.StreamReader对象将包含文件写到HTTP内容流中//me:网上说asp.net中用include也可以的
asp.net中html标签代码:复制代码代码如下:普通的html标签:复制代码代码如下:对于ASP.NET或普通的HTML标签,下面JS代码都适用:复制代码代
本文实例讲述了ASP.NET缓存处理类。分享给大家供大家参考。具体如下:ASP.NET缓存处理类。用法:Justcopythiscodeintoanewclas