一.default.aspx 代码参考:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Application.Lock();
Application["counter"] = (int)Application["counter"] + 1;
Application.UnLock();
labTip2.Text = Application["counter"].ToString();
}
}
二.Login.aspx代码参考:
protected void btnLogin_Click(object sender, EventArgs e)
{
if (txtName.Text.Trim().Equals("admin") && txtPass.Text.Trim().Equals("1111"))
{
Session["username"] = txtName.Text.Trim();
if (ckSavePass.Checked)
{
if (Request.Cookies["username"] == null)
{
Response.Cookies["username"].Expires = DateTime.Now.AddDays(30);
Response.Cookies["userpwd"].Expires = DateTime.Now.AddDays(30);
Response.Cookies["username"].Value = txtName.Text.Trim();
Response.Cookies["userpwd"].Value = txtPass.Text.Trim();
}
}
Response.Redirect("UserContent.aspx");
}
else
{
//ClientScript.RegisterStartupScript(this.GetType(), "", "alert('用户名或密码错误!');", true);
labTip.Text = "用户名或密码错误!";
}
}
protected void btnReset_Click(object sender, EventArgs e)
{
txtName.Text = "";
txtPass.Text = "";
}
protected void txtName_TextChanged(object sender, EventArgs e)
{
if (Request.Cookies["username"] != null)
{
if (Request.Cookies["username"].Value.Equals(txtName.Text.Trim()))
{
txtPass.Attributes["value"] = Request.Cookies["userpwd"].Value;
}
else
{
txtPass.Attributes["value"] = "";
}
}
}
三.UserContent.aspx参考代码:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] != null)
labTip.Text = Session["username"].ToString();
else
Response.Redirect("Login.aspx");
}
protected void btnReset_Click(object sender, EventArgs e)
{
Session.Clear();
Response.Redirect("default.aspx");
}
四.global.asax参考代码:
void Application_Start(object sender, EventArgs e)
{
//在应用程序启动时运行的代码
Application["counter"] = 0;
}