[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

RegisterStartupScript Alert and stop

Posted on 2012-08-28
5
Medium Priority
?
1,050 Views
Last Modified: 2012-08-30
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

0
Comment
Question by:webressurs
5 Comments
 
LVL 2

Accepted Solution

by:
ransommule earned 2000 total points
ID: 38341303
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
 
LVL 1

Author Comment

by:webressurs
ID: 38341765
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
 
LVL 18

Expert Comment

by:Rajar Ahmed
ID: 38341955
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
 
LVL 26

Expert Comment

by:Alan Warren
ID: 38342699
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
 
LVL 2

Assisted Solution

by:ransommule
ransommule earned 2000 total points
ID: 38346652
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month19 days, 10 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question