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.
LVL 1
jjrr007Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

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.