关于checkbox无法保存其状态的问题这个这个问题是刚才一个关于cookie的帖子的问题的延伸,还是另外发帖吧一
关于checkbox无法保存其状态的问题
这个这个问题是刚才一个关于cookie的帖子的问题的延伸,还是另外发帖吧
一个登录的窗体,有一个checkbox,用来选择是否保存用户名密码,一个button用来提交
C# codeprotected void Page_Load(object sender, EventArgs e) { if (Request.Cookies["travians"] != null) { TextBox1.Text = Request.Cookies["travians"]["userid"]; TextBox2.Text = Request.Cookies["travians"]["userpwd"]; CheckBox1.Checked = true; } else { CheckBox1.Checked = false; } if (IsPostBack) { if (CheckBox1.Checked == true) { if (Response.Cookies["travians"] != null) { HttpCookie cookie = new HttpCookie("travians"); cookie.Expires = DateTime.Now.AddDays(-1); Response.Cookies.Add(cookie); } Response.Cookies["travians"]["userid"] = TextBox1.Text; Response.Cookies["travians"]["userpwd"] = TextBox2.Text; Response.Cookies["travians"].Expires = DateTime.MaxValue; } Session["userid"] = TextBox1.Text; Server.Transfer("default.aspx"); } }
现在的问题是我把checkbox的勾子勾上以后,点button,page_load事件里认为CheckBox1.Checked 是false,所以不会执行保存cookie的语句,直接跳转到default.aspx页面了。CheckBox1的ecableviewstate属性是设成true的,望各位指点,谢谢
[解决办法]protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.Cookies["travians"] != null)
{
TextBox1.Text = Request.Cookies["travians"]["userid"];
TextBox2.Text = Request.Cookies["travians"]["userpwd"];
CheckBox1.Checked = true;
}
else
{
CheckBox1.Checked = false;
}
if (CheckBox1.Checked == true)
{
if (Response.Cookies["travians"] != null)
{
HttpCookie cookie = new HttpCookie("travians");
cookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(cookie);
}
Response.Cookies["travians"]["userid"] = TextBox1.Text;
Response.Cookies["travians"]["userpwd"] = TextBox2.Text;
Response.Cookies["travians"].Expires = DateTime.MaxValue;
}
Session["userid"] = TextBox1.Text;
Server.Transfer("default.aspx");
}
}
楼主可以试试
不行的话,把问题描述的再清楚一点,继续关注