views:

107

answers:

3
protected void LoginCheck()
{
    if (Session["Login_Status"] == null || Session["Login_Status"] == "false")
    {
        MessageBoxShow("Please Login!");
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    LoginCheck();
    Session.RemoveAll();
    Button1.PostBackUrl = "~/Default.aspx";
    Response.Redirect("~/Default.aspx");
}

how to not process

    Session.RemoveAll();
    Button1.PostBackUrl = "~/Default.aspx";
    Response.Redirect("~/Default.aspx");

when Login false

how do I code this program not to use a condition if (LoginCheck()) { }. I'd like it to work like php: php exit; function ?

+3  A: 

Have LoginCheck return a boolean value indicating success or failure like this:

protected Boolean LoginCheck()
{
    if (Session["Login_Status"] == null || Session["Login_Status"] == "false")
    {
        MessageBoxShow("Please Login!");
        return false;
    }
    return true;
}

Then you can use that value to determine whether or not to do anything else like this:

protected void Button1_Click(object sender, EventArgs e)
{
    if (LoginCheck()) 
    {
        Session.RemoveAll();
        Button1.PostBackUrl = "~/Default.aspx";
        Response.Redirect("~/Default.aspx");
    }
}
Andrew Hare
how codeing pro grame not use condition if (LoginCheck()) { }like php exit; function ?
monkey_boys
I am sorry, I don't understand the question.
Andrew Hare
He wants his LoginCheck function to work like PHP's Exit function, which ends the request and stops execution and flushes the response.
Anderson Imes
+1  A: 

I think this is what you want (but it's not that good of an idea):

protected void LoginCheck()
{
    if (Session["Login_Status"] == null || Session["Login_Status"] == "false")
    {
        MessageBoxShow("Please Login!");
        Response.End();
    }
}
Anderson Imes
Error 3 No overload for method 'End' takes '1' argument
monkey_boys
I modified it. There is an overload that tells it not to throw an exception, but I can't remember it offhand. You can probably look into it if it's a problem
Anderson Imes
Also your accept rate is really low (5%). You know how to accept an answer, right?
Anderson Imes
Here's a good tutorial: http://meta.stackoverflow.com/questions/8396/how-do-i-accept-an-answer-where-do-i-click
Anderson Imes
A: 

This is a bad idea

If some one is reading your code, they are going to assume that after Logincheck() that Session.RemoveAll() ... will be called. If you do some trick in LoginCheck that changes that it will be very confusing reading Button_Click1(...)

Nick