jeudi 17 novembre 2016

asp.net checkbox.checked evaluation after uncheck

        protected void submitLogin(object sender, EventArgs e)
    {

        string connStr = ConfigurationManager.ConnectionStrings["tropicalConnectionString"].ConnectionString;
        string queryString = "select * from tblUserLogin where userId='" + useridtextbox.Text
            + "' and password ='" + passwordtextbox.Text + "'";
        Console.WriteLine(queryString);
        conn = new OleDbConnection(connStr);

        conn.Open();
        cmd = new OleDbCommand(queryString, conn);
        reader = cmd.ExecuteReader();
        userId = "";
        if (reader.HasRows && reader.Read())
        {
            userId = reader.GetString(reader.GetOrdinal("UserId"));
        }
        if (reader.HasRows)
        {
            Session["uid"] = userId;
            Response.BufferOutput = true;
            Response.Redirect("/UI/Products.aspx", false);
            bool ck = remembercheckbox.Checked;


            if (ck)
            {

                Response.Cookies["UserName"].Expires =  DateTime.Now.AddDays(7);
                Response.Cookies["UserName"].Value = useridtextbox.Text.Trim();
            }
            else
            {
                HttpCookie myCookie = new HttpCookie("UserSettings");
                Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(-1);
                Response.Cookies.Add(myCookie);
            }
        }
        else
        {
            passwordtextbox.Text = "invalid user";
        }

Here's a snippet of my login content page

                   <div class="login">
                    <asp:CheckBox ID="remembercheckbox" TextAlign="left" runat="server" text="Remember my Id" checked="false" OnCheckedChanged="clickCkbx" />
                    <asp:Button ID="loginButton" CssClass="loginButton" runat="server" text="Login" OnClick="submitLogin" /> 
                </div>  

I'm trying to implement a remember user id function and practicing it with cookie. The problem is that when I uncheck the box for "remembercheckbox", the value in remembercheckbox.Checked keep returning true and thus I cannot go into the else statement and destroy the cookie.

Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire