Solved

MessageBox not showing message before response.redirect happens

Posted on 2010-08-17
12
1,725 Views
Last Modified: 2012-06-21
Our web app has multiple pages.  Default.aspx is meant to be the start page.  I'm trying to code something to guard against a user bookmarking another page such as Actions.aspx.  Session["intCaseID"] is set in the Default.aspx page so if it is null then the user must have bookmarked the Actions.aspx page.

The code below will transfer a user to Default.aspx but will not display the message box.  I expected the page to wait until the user had clicked the "OK" button before it executed the response.redirect.  Why did it not wait?

Thx,

Eric
if (Session["intCaseID"] == null)
        {
            Utility.MessageBox("Redirecting you to the start page...");
            Response.Redirect("Default.aspx");
        }

Open in new window

0
Comment
Question by:Eric3141
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 33454220
what is Utility.MessageBox?is it windows.forms.MessageBox or a custom one?
0
 
LVL 2

Author Comment

by:Eric3141
ID: 33454224
The MessageBox method is contained in a custom class called Utility. The code is below.

public static class Utility
{

public static void MessageBox(string MyMessage)
{
//This method pops a message box onto the screen to display whatever
//text you pass into the method via the input parameter.
string ScriptString = "alert('" + MyMessage + "');";

System.Web.UI.Page page = HttpContext.Current.Handler as System.Web.UI.Page;
page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "ClientScript", ScriptString, true);

}
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 33454332
try:

ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('"+MyMessage +".');window.open('AnotherPage.aspx','_self');", true);
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 42

Expert Comment

by:sedgwick
ID: 33454390
sorry, u need to integrate the "redirect" in the script:

string ScriptString = "<scriptlanguage='JavaScript'>alert('" + MyMessage + "');window.location.href =
'Default.aspx';</script>";
page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "ClientScript", ScriptString, true);

check http://bytes.com/topic/asp-net/answers/730759-show-alert-box-then-redirect
0
 
LVL 2

Author Comment

by:Eric3141
ID: 33454426
The first one you gave worked.  The 2nd one gave this error:
 The name 'page' does not exist in the current context  
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 33454588
just remove the 'page':
string ScriptString = "<scriptlanguage='JavaScript'>alert('" + MyMessage + "');window.location.href =
'Default.aspx';</script>";
ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "ClientScript", ScriptString, true);
0
 
LVL 2

Author Comment

by:Eric3141
ID: 33454662
Now it says "A runtime error has occurred.  Do you wish to debug?"

If this is getting to be too much trouble for you I can just use the first suggestion you gave which worked though there was a slight hesitation before the redirect happened.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 33454694
the runtime error occur cause i probably misspelled the script.
if the 1st suggestion works for you, and suit your needs than use it :)
0
 
LVL 2

Author Comment

by:Eric3141
ID: 33454713
I hate to ask now but what would the 2nd script (that gave the error) do that the first would not?  Were there any real advantages to it?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 33454766
the 2nd suggestion simply use the 'Redirect.Response' in the javascript itself.
they both have the same result, no a meaningful difference either way.
0
 
LVL 2

Author Comment

by:Eric3141
ID: 33455396
Sedqwick:

I was able to take the first code you gave me and create a custom class so I can call this from multple pages.  Thanks for the help!


  public static void WrongSiteEntry()
  {
      //This method handles the case where someone has entered the site via a page other than
      //Default.aspx.  This happens when they bookmark one of the other pages besides Default.aspx.
      //It gives them a message box telling them they being redirected to the start page then
      //redirects them to Default.aspx

      string MyMessage = "Redirecting you to the start page...";
      string ScriptString = "alert('" + MyMessage + ".');window.open('Default.aspx','_self');";

      System.Web.UI.Page page = HttpContext.Current.Handler as System.Web.UI.Page;
      page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "ClientScript", ScriptString, true);
  }
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 33455422
glad it was helpful. :)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 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