1.新建一个pagebase类,然后让页面继承pagebase,
2.新建一个页面,继承PageBase,然后面页面添加一个Label1且Text属性为0;
3.添加一个Button,然后运行页面,点击button二次,然后,再按F5刷新页面三次,
4.查看根目录下的a.txt,和b.txt
效果值为 a.txt : 1 ,2 b.txt :1,2,2,2,2 说明页面刷新时执行了b.txt
代码如下:
public class PageBase : System.Web.UI.Page
{
private string _strSessionKey;
private string _hiddenfieldName;
private string _strLastViewstate;
public PageBase()
{
_hiddenfieldName = "__LastVIEWSTATE_SessionKey";
_strSessionKey = System.Guid.NewGuid().ToString();
_strLastViewstate = string.Empty;
}
public bool IsRefreshed
{
get
{
string str1 = GetSessinContent();
_strLastViewstate = str1;
string str2 = this.Session[GetSessinKey()] as string;
bool flag1 = (str1 != null) && (str2 != null) && (str1 == str2);
return flag1;
}
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
string str = GetSessinKey();
this.Session[str] = _strLastViewstate;
this.RegisterHiddenField(_hiddenfieldName, str);
base.Render(writer);
}
private string GetSessinKey()
{
string str = this.Request.Form[_hiddenfieldName];
return (str == null) ? _strSessionKey : str;
}
private string GetSessinContent()
{
string str = this.Request.Form["__VIEWSTATE"];
if (str == null)
{
return null;
}
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
}
}
{
private string _strSessionKey;
private string _hiddenfieldName;
private string _strLastViewstate;
public PageBase()
{
_hiddenfieldName = "__LastVIEWSTATE_SessionKey";
_strSessionKey = System.Guid.NewGuid().ToString();
_strLastViewstate = string.Empty;
}
public bool IsRefreshed
{
get
{
string str1 = GetSessinContent();
_strLastViewstate = str1;
string str2 = this.Session[GetSessinKey()] as string;
bool flag1 = (str1 != null) && (str2 != null) && (str1 == str2);
return flag1;
}
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
string str = GetSessinKey();
this.Session[str] = _strLastViewstate;
this.RegisterHiddenField(_hiddenfieldName, str);
base.Render(writer);
}
private string GetSessinKey()
{
string str = this.Request.Form[_hiddenfieldName];
return (str == null) ? _strSessionKey : str;
}
private string GetSessinContent()
{
string str = this.Request.Form["__VIEWSTATE"];
if (str == null)
{
return null;
}
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
}
}
页面代码:
protected void Page_Load(object sender, EventArgs e)
{
int i = int.Parse(Label1.Text) + 1;
Label1.Text = i.ToString();
if (!this.IsRefreshed)
{
WriteFile("a.txt", i.ToString());
}
WriteFile("b.txt", i.ToString());
}
private void WriteFile(string strFileName, string strContent)
{
string str = this.Server.MapPath(strFileName);
System.IO.StreamWriter sw = System.IO.File.AppendText(str);
sw.WriteLine(strContent);
sw.Flush();
sw.Close();
}
{
int i = int.Parse(Label1.Text) + 1;
Label1.Text = i.ToString();
if (!this.IsRefreshed)
{
WriteFile("a.txt", i.ToString());
}
WriteFile("b.txt", i.ToString());
}
private void WriteFile(string strFileName, string strContent)
{
string str = this.Server.MapPath(strFileName);
System.IO.StreamWriter sw = System.IO.File.AppendText(str);
sw.WriteLine(strContent);
sw.Flush();
sw.Close();
}