How to Exit Method When Not for used this Method in Method
-
05-07-2019 - |
Question
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 ?
Solution
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");
}
}
OTHER TIPS
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();
}
}
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(...)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow