前提准备条件:
1。你要有2个TextBox控件,一个用来输密码,一个用来输用户名,不过好想这些都是必需的,
2。你要有一个 RadioButtonList 控件,用来设置Cookie的存活时间。关于内部值得设置是:年y,月m,星期w,天d,浏览器进程p。
3。好了,现在你就可以在网页pageload中嵌入以下代码了:
注意:据说asp.net中,没有内置的设置Focus的方法,请自己使用javascript 控件名.Focus实现
public void check(System.Web.UI.WebControls.TextBox tb,System.Web.UI.WebControls.RadioButtonList choose) { HttpCookie hc = Request.Cookies["TestCookie"]; if(hc !=null) tb.Text = hc.Value; else { hc = new HttpCookie("TestCookie",tb.Text); DateTime dt =DateTime.Now; TimeSpan ts = new TimeSpan(365,0,0,0); if(choose.SelectedItem.Value =="y") ts = new TimeSpan(365,0,0,0); else if(choose.SelectedItem.Value =="m") ts = new TimeSpan(30,0,0,0); else if(choose.SelectedItem.Value =="w") ts = new TimeSpan(7,0,0,0); else if(choose.SelectedItem.Value =="d") ts = new TimeSpan(1,0,0,0); hc.Expires = dt.Add(ts); Response.Cookies.Add(hc); } }
|