WebForm学习笔记
1.判断回发还是首次加载:Page.IsPostBack,在Page_Load函数中判断;
eg:
if(!Page.IsPostBack)
{
//首次加载
}
else
{
//回发
}
2.QueryString属性和Params属性;
eg:
<div>
<a href="default1.html?name=MrChen&age=20">第二个页面</a>
</div>
string name = Request.QueryString["name"];
string age = Request.QueryString["age"];
或
string name = Request.Params["name"];
string age = Request.Params["age"];
3.ViewState对象的使用(object类型),viewstate是页面级别的.只要这个页面在,viewstate中存的数据就在,因为ViewStatic是保存在客户端所以用户每次与服务器端交互都要额外的传递ViewStatic里的信息,这样就加大了服务器的流量 ;
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
ViewState["mCount"] = 0;//首次加载
}
}
//int count = 0;
protected void btn_Cal_Click(object sender, EventArgs e)
{
int count = (int)ViewState["mCount"];
count++;
Label1.Text = count.ToString();
ViewState["mCount"] = count;
}
4.Session对象的使用(object类型),不同页面传递信息;
eg:
`List< string> Name = new List< string>();
Session["Name"] = Name;
5.跳转页面;
Response.Redirect(“default.aspx”);
6.Cookie对象
存储少量不重要的数据;
存储在客户端的文本文件中(须设置有效期);
安全性差;
存储的数据类型------字符串;
与浏览器窗口无关,但与访问的站点有关;
具体特定的过期时间和日期;
在客户端存储后,将随着浏览器对相关网站页面请求而一并发送到web服务器;
创建方法一:Response.Cookies[Cookie的名称].Value = 变量值;
创建方法二:HttpCookie = new HttpCookie(名称,变量);
读取方法:string 变量名=Request.Cookies[名称].Value;
eg:
Response.Cookies[“Name”].Expires=DateTime.Now.AddDays(1.0);
Response.Cookies[“Name”].Value=this.txtUserName.Text.Trim();
7.Application对象;