Question

I have a form on my page where users can send an email,

        try
        {
            smtpClient.Send(message);

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Key", 
                "alert('Thank you for your submission.');", true);
        }
        catch(Exception)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Key", 
                "alert('There was an error processing your request.');", true);
        }

        Response.Redirect("home.aspx");

The alert does not appear and the page simply redirects immediately, unless I comment out Response.Redirect, then the alert works. I want to display the alert and then redirect.

How can this be achieved? I thought of using a timer somehow, but I'm not sure if thats the best solution.

Thanks.

Était-ce utile?

La solution

Let the JS do the redirect, instead of using Response.Redirect.

eg.

alert('....'); window.location='home.aspx';

Autres conseils

The reason is that the RegisterClientScriptBlock call and the redirect code are within the same page life cycle. That means Response.Redirect() get invoked before the page rendered to the client's browser. The client has no way to see alert message.

The solution is use JS to redirect page as suggested by Will

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top