2

I have a login box, made with jquery with text boxes and then an asp:button, that validates and makes the sessions, like in the code.

I have made that a label appears when the username and password is wrong and you click the asp:button, but because of the post back the login box disappears.

 protected void Button1_Click(object sender, EventArgs e)
{
   Connectons etc...
    cmd.CommandText = "SELECT * FROM Users WHERE Email = @email AND password = @pass";
    cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = username.Text;
    cmd.Parameters.Add("@pass", SqlDbType.VarChar).Value = password.Text;
    conn.Open();
    SqlDataReader reader = cmd.ExecuteReader();
    if (reader.Read())
    {
        Session["user_id"] = reader["user_id"];
        Session["username"] = reader["Username"];
        Session["name"] = reader["Name"];
        Session["password"] = reader["Password"];
        Session["LoggedIn"] = true;
        Session["role"] = reader["Role_id"];
        if (Convert.ToInt32(reader["Role_id"]) == 1)
        {
   Response.Redirect("Admin/default.aspx");
        }

        else
        {
            Response.Redirect("default.aspx");
        }

    }

    else
    {

        Label1.Text = "Login is wrong";

    }

    conn.Close();


}

How do i make the button not post back if the login is wrong?

        <a id="modal_trigger" href="#modal" class="btn btn-default">Log ind</a>

    <div id="modal" class="popupContainer" style="display: none;"> </div>

         $("#modal_trigger").leanModal({
        top: 100,
        overlay: 0.6,
        closeButton: ".modal_close"
    });
Caroline Olivia
  • 336
  • 3
  • 12

3 Answers3

1

In button property add AutoPostBack=False

Note: as per your question it is not clear what you want to achieve but as you have asked how to add autopostback to false , so i gave the above answer. If you want to prevent postback if the login is incorrect then it is not possible because the validation of credential of user happen only after the button is clicked.

Vishwa Ratna
  • 5,567
  • 5
  • 33
  • 55
1

A Postback needs to be called to reach the code you have up there. You could use some ajax to query your db onClick and cause a postback when successful I believe. I don't have an example. sorry.

Mike Wight
  • 56
  • 1
  • 4
  • 8
1

Your problem is not in the postback, it's in the fact that you're using a predefined login box that is only rendered when the page is not in postback state. Essentially, the rendering code is assuming that every login will be successful.

To fix this, you need to find where/when/how the predefined login "box" is created, and add the line to recreate it right after your "login incorrect" text.

In this manner, the login "box" will re-appear if the first attempt failed.

 if (Convert.ToInt32(reader["Role_id"]) == 1)
    {
        Response.Redirect("Admin/default.aspx");
    }

    else
    {
        Response.Redirect("default.aspx");
    }

}

else
{

    reDraw = true;
}

//this has page scope (outside all methods)
bool reDraw = false;

private void reDrawLogin(){
      Label1.Text = "Login is wrong";
    //re-render login box to allow for another try
    string myScript = "\n<script type=\"text/javascript\" language=\"Javascript\" id=\"EventScriptBlock\">\n";
    myScript += "style="display: block;visibility:visible"> 
    myScript += "\n\n </script>";
 Page.ClientScript.RegisterStartupScript(this.GetType(), "myKey", myScript, false);
 }

 //in your page_load, add:

 if(isPostBack){
    if(reDraw){
       reDrawLogin();
       reDraw=false;
    }
 }

and don't foret the USING in your CONN and COM!!! Good luck.

Shannon Holsinger
  • 2,293
  • 1
  • 15
  • 21