Inside the Submit button click event write these code instate of writing in page load event.
if (chk_rem.Checked == true)
{
Response.Cookies["UName"].Value = txt_username.Text;
Response.Cookies["PWD"].Value = txt_password.Text;
Response.Cookies["UName"].Expires = DateTime.Now.AddMonths(2);
Response.Cookies["PWD"].Expires = DateTime.Now.AddMonths(2);
}
else
{
Response.Cookies["UName"].Expires = DateTime.Now.AddMonths(-1);
Response.Cookies["PWD"].Expires = DateTime.Now.AddMonths(-1);
}
Suggestion:
Please don't store UserName and Password directly inside the cookie.
Either encrypt the username and password or store a GUID .