[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 8050
  • Last Modified:

Page Redirect After Clicking OK on Message Box

I am using he following code C# with a web form:

 if (result == 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Error! Add New Item First!');", true);
      Response.Redirect("~/new.aspx");
            }

Open in new window


The issue I have is the page just redirects.  It doesn't first display the message box and then after I click OK, then redirect.
0
jjrr007
Asked:
jjrr007
  • 3
  • 2
  • 2
1 Solution
 
käµfm³d 👽Commented:
Of course it redirects!!  The request to redirect came directly from the server. Any time you issue a Response.Redirect, you are telling the client: "Forget any of the HTML I've sent you, and go here." What you need to do is include an additional bit of Javascript in your script.

e.g.

if (result == 0)
{
    ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Error! Add New Item First!'); window.location = '" + Page.ResolveUrl("~/new.aspx") + "';", true);
}

Open in new window


You can see that I've include an assignment to the window.location property. This should cause the client to effectively redirect itself once the alert box is closed. That property should be ubiquitous, but you may want to double-check that it's cross-browser. (I believe it is.)
0
 
Mandeep SinghDatabase AdministratorCommented:
try this it is working fine.

if (result == 0)
{
string message = "You will now be redirected to Google Home Page.";
    string url = "http://www.google.com";
    string script = "window.onload = function(){ alert('";
    script += message;
    script += "');";
    script += "window.location = '";
    script += url;
    script += "'; }";
    ClientScript.RegisterStartupScript(this.GetType(), "Redirect", script, true);
}

Open in new window

0
 
jjrr007Author Commented:
Thanks.  I tried this and thesame page refreshed- no message or page change.  Below is what I used:

  if (result == 0)
            {
string message = "Error! Add New Item First. Item Does Not Exist! Being Redirected";
                string url = "http://www.google.com";
                string script = "window.onload = function(){ alert('";
                script += message;
                script += url;
                script += "'; }";
                ClientScript.RegisterStartupScript(this.GetType(), "Redirect", script, true);
}

Open in new window


What do you suggest?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
käµfm³d 👽Commented:
I take it you did not see my post?
0
 
Mandeep SinghDatabase AdministratorCommented:
0
 
jjrr007Author Commented:
Yes, I missed your comment Kaufmed.  It worked.  You are the best
0
 
jjrr007Author Commented:
Thank you for a correct, quick answer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now