RegisterStartupScript Alert and stop

How can I show an Alert box fromCode Behind, and stop the code when "OK" is clicked?

Please see attached code. If btnBlock_OnClick() runs and the user is logged out the LoginCheck() shall display an Alert box and the code shall stop after clicking "OK". The method AddBlockProfile() shall not run if user is logged out (alert is shown).

In the attached code the AddBlockProfile() method runs even if the user is logged out...


protected void LoginCheck()
{
    if (!HttpContext.Current.User.Identity.IsAuthenticated)
    {
        ClientScriptManager cr = Page.ClientScript;
        string scriptStr = "alert('You are logged out, operation stopped'); return false;";
        cr.RegisterStartupScript(typeof(Page), "Message", scriptStr, true);
    }
}

protected void btnBlock_OnClick(object sender, EventArgs e)
{
    LoginCheck();
    AddBlockProfile();
}

Open in new window

LVL 1
webressursAsked:
Who is Participating?
 
ransommuleCommented:
protected void btnBlock_OnClick(object sender, EventArgs e) {
    if( HttpContext.Current.User.Identity.IsAuthenticated ) {
        AddBlockProfile();
    } else  {
        ClientScriptManager cr = Page.ClientScript;
        string scriptStr = "alert('You are logged out, operation stopped'); return false;";
        cr.RegisterStartupScript(typeof(Page), "Message", scriptStr, true);
     }
}
0
 
webressursAuthor Commented:
Yes, I know that works but I need to do it "my" way. The reason is that this check is done many places, and I just want to add LoginCheck(); each place the check shall be done...

Like this:

protected void btnBlock_OnClick(object sender, EventArgs e)
{
    LoginCheck();
    AddBlockProfile();
}

protected void btnDelete_OnClick(object sender, EventArgs e)
{
    LoginCheck();
    DeleteProfile();
}

protected void btnUpdate_OnClick(object sender, EventArgs e)
{
    LoginCheck();
    UpdateProfile();
}

.
.
.
.

Open in new window

0
 
Rajar AhmedConsultantCommented:
I need to do it "my" way ??
Only , if your are redirecting you can some how achieve "your" way otherwise you still have to do the  boolean check in all the code .
if (!HttpContext.Current.User.Identity.IsAuthenticated)
    {
        ClientScriptManager cr = Page.ClientScript;
        //string scriptStr = "alert('You are logged out, operation stopped'); return false;";
        //cr.RegisterStartupScript(typeof(Page), "Message", scriptStr, true);
       //Are you redirecting ? when it fails ?
       try 
           {
               Response.Redirect("invalidsession.aspx");
            }
catch(Exception ex){ 
//Skip thread abort exception so it proceeds directly
  }
    }

Open in new window

0
 
Alan WarrenApplications DeveloperCommented:
Hi webressurs,
Perhaps something like this:
protected void btnBlock_OnClick(object sender, EventArgs e)
{
	LoginCheck();
	AddBlockProfile();

  }

  protected void btnDelete_OnClick(object sender, EventArgs e)
  {
	LoginCheck();
	DeleteProfile();
  }

  protected void btnUpdate_OnClick(object sender, EventArgs e)
  {
	LoginCheck();
	UpdateProfile();
  }

  public bool LoginCheck()
  {

	if (!My.User.IsAuthenticated)  // or HttpContext.Current.User.Identity.IsAuthenticated
	{
	  // raise your javascript alert and return false
	  // Define the name and type of the client scripts on the page. 
	  string csname1 = "Login Required";
	  Type cstype = this.GetType();

	  // Get a ClientScriptManager reference from the Page class. 
	  ClientScriptManager cs = Page.ClientScript;

	  // Check to see if the startup script is already registered. 
	  if (!(cs.IsStartupScriptRegistered(cstype, csname1)))
	  {
		StringBuilder cstext1 = new StringBuilder();
		cstext1.Append("<script type=text/javascript> " + string.Format("alert('Your login session has expired, the operation was aborted!');") + "</");
		cstext1.Append("script>");

		cs.RegisterStartupScript(cstype, csname1, cstext1.ToString());
	  }
	}

	return My.User.IsAuthenticated;
  }

Open in new window

0
 
ransommuleCommented:
I dont know if it is posible, but I think you shouldt do it "your way".

It is like inserting a goto instruction in the middle of your code.

http://en.wikipedia.org/wiki/Spaghetti_code

Instead you could do something like this,....

protected void ShowErrorToUser() {
        ClientScriptManager cr = Page.ClientScript;
        string scriptStr = "alert('You are logged out, operation stopped'); return false;";
        cr.RegisterStartupScript(typeof(Page), "Message", scriptStr, true);
}

protected void btnBlock_OnClick(object sender, EventArgs e)
{
   if( HttpContext.Current.User.Identity.IsAuthenticated)
    {
          BlockProfile();
    }   else {
         ShowErrorToUser();
    }
}

protected void btnDelete_OnClick(object sender, EventArgs e)
{
    if( HttpContext.Current.User.Identity.IsAuthenticated)
    {
          DeleteProfile();
    }   else {
         ShowErrorToUser();
    }
}

protected void btnUpdate_OnClick(object sender, EventArgs e)
{
   if( HttpContext.Current.User.Identity.IsAuthenticated)
    {
          UpdateProfile();
    }   else {
         ShowErrorToUser();
    }
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.