3

I am simply trying to show message first then redirect the user to another page. The issue i am having is that it is not showing the message first but it is redirecting the page right a way. Here is my code

if (some condition == true)
{
    string message = string.Empty;
    message = "Success.  Please check your email.  Thanks.";
    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);

    Response.Redirect("Login.aspx");    
}
1

4 Answers 4

3

The best way to achieve your result is doing it asynchronously with Javascript (client-side).

If you want do it server-side, here is an example:

protected void btnRedirect_Click(object sender, EventArgs e)
{
    string message = "You will now be redirected to YOUR Page.";
    string url = "http://www.yourpage.com/";
    string script = "window.onload = function(){ alert('";
    script += message;
    script += "');";
    script += "window.location = '";
    script += url;
    script += "'; }";
    ClientScript.RegisterStartupScript(this.GetType(), "Redirect", script, true);
}
Sign up to request clarification or add additional context in comments.

Comments

0

That is because your code redirects from server-side, even before that script reaches the client browser. You should remove that redirect and modify your javascript so that the redirect is done at the client side AFTER that message is displayed.

EDIT: You should definitely check on ASP.NET page life cycle.

Comments

0

The issue here is that you're doing two things:

  1. Adding a script to the output that is sent to the browser that presents a JavaScript alert.
  2. Using Response.Redirect to trigger an HTTP 302 redirect.

The latter (2) means that (1) doesn't actually do anything. To achieve what you want here, you could send a script down using RegisterStartupScript like:

alert('Message');
window.location.href = 'login.aspx';

So you'd remove the Response.Redirect line and use:

ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "'); window.location.href = 'login.aspx'", true);

Comments

0

You can use a Timer in C#. Just give the user enough time to read the message, then redirect your user to your desired page.

This thread has a good example in using the timer: how to use Timer in C#

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.