Eric3141
asked on
MessageBox not showing message before response.redirect happens
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
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");
}
what is Utility.MessageBox?is it windows.forms.MessageBox or a custom one?
ASKER
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.Handle r as System.Web.UI.Page;
page.ClientScript.Register StartupScr ipt(typeof (System.We b.UI.Page) , "ClientScript", ScriptString, true);
}
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.Handle
page.ClientScript.Register
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sorry, u need to integrate the "redirect" in the script:
string ScriptString = "<scriptlanguage='JavaScri pt'>alert( '" + MyMessage + "');window.location.href =
'Default.aspx';</script>";
page.ClientScript.Register StartupScr ipt(typeof (System.We b.UI.Page) , "ClientScript", ScriptString, true);
check http://bytes.com/topic/asp-net/answers/730759-show-alert-box-then-redirect
string ScriptString = "<scriptlanguage='JavaScri
'Default.aspx';</script>";
page.ClientScript.Register
check http://bytes.com/topic/asp-net/answers/730759-show-alert-box-then-redirect
ASKER
The first one you gave worked. The 2nd one gave this error:
The name 'page' does not exist in the current context
The name 'page' does not exist in the current context
just remove the 'page':
string ScriptString = "<scriptlanguage='JavaScri pt'>alert( '" + MyMessage + "');window.location.href =
'Default.aspx';</script>";
ClientScript.RegisterStart upScript(t ypeof(Syst em.Web.UI. Page), "ClientScript", ScriptString, true);
string ScriptString = "<scriptlanguage='JavaScri
'Default.aspx';</script>";
ClientScript.RegisterStart
ASKER
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.
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.
the runtime error occur cause i probably misspelled the script.
if the 1st suggestion works for you, and suit your needs than use it :)
if the 1st suggestion works for you, and suit your needs than use it :)
ASKER
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?
the 2nd suggestion simply use the 'Redirect.Response' in the javascript itself.
they both have the same result, no a meaningful difference either way.
they both have the same result, no a meaningful difference either way.
ASKER
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','_se lf');";
System.Web.UI.Page page = HttpContext.Current.Handle r as System.Web.UI.Page;
page.ClientScript.Register StartupScr ipt(typeof (System.We b.UI.Page) , "ClientScript", ScriptString, true);
}
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.
System.Web.UI.Page page = HttpContext.Current.Handle
page.ClientScript.Register
}
glad it was helpful. :)