Alert and Forwarding

I'm stuck right now. I have a web form with a button that registers or saves an entry. I would like it to display a javascript warning and then redirect to the page. Here is the code I'm using

protected void Save(..) { // Do save stuff DisplayAlert("The changes were saved Successfully"); Response.Redirect("Default.aspx"); } 

This code simply redirects without prompting "Saved successfully."

Here is my DisplayAlert code

  protected virtual void DisplayAlert(string message) { ClientScript.RegisterStartupScript( this.GetType(), Guid.NewGuid().ToString(), string.Format("alert('{0}');", message.Replace("'", @"\'").Replace("\n", "\\n").Replace("\r", "\\r")), true ); } 

Can someone help me find a solution?

thank

+7
javascript redirect alert
Apr 23 '09 at 15:47
source share
4 answers

You cannot execute Response.Redirect because your javascript warning will never be displayed. It is better to have javascript code actually do windows.location.href='default.aspx' to redirect after a warning is displayed. Something like that:

 protected virtual void DisplayAlert(string message) { ClientScript.RegisterStartupScript( this.GetType(), Guid.NewGuid().ToString(), string.Format("alert('{0}');window.location.href = 'default.aspx'", message.Replace("'", @"\'").Replace("\n", "\\n").Replace("\r", "\\r")), true); } 
+7
Apr 23 '09 at 15:51
source share

The DisplayAlert method adds a client script to the current executable page request. When you call Response.Redirect, ASP.NET issues an HTTP 301 redirect to the browser, so it launches a new page request where the registered client script no longer exists.

Since your code is running on the server side, there is no way to display the client side of the alert and redirect.

Also, displaying a JavaScript warning window can be confusing for a user workflow, an inline message would be much more preferable. Perhaps you could add a message to the session and display this on the request of the Default.aspx page.

 protected void Save(..) { // Do save stuff Session["StatusMessage"] = "The changes were saved Successfully"; Response.Redirect("Default.aspx"); } 

Then, in the Default.aspx.cs code behind (or the common base class of the page, so that this can happen on any page or even on the main page):

 protected void Page_Load(object sender, EventArgs e) { if(!string.IsNullOrEmpty((string)Session["StatusMessage"])) { string message = (string)Session["StatusMessage"]; // Clear the session variable Session["StatusMessage"] = null; // Enable some control to display the message (control is likely on the master page) Label messageLabel = (Label)FindControl("MessageLabel"); messageLabel.Visible = true; messageLabel.Text = message; } } 

The code is not verified, but should point in the right direction

+4
Apr 24 '09 at 8:34
source share

This works great:

 string url = "home.aspx"; ClientScript.RegisterStartupScript(this.GetType(), "callfunction", "alert('Saved Sucessfully.');window.location.href = '" + url + "';",true); 
+4
Feb 08 2018-12-12T00:
source share
 protected void Save(..) { // Do save stuff ShowMessageBox(); } private void ShowMessageBox() { string sJavaScript = "<script language=javascript>\n"; sJavaScript += "var agree;\n"; sJavaScript += "agree = confirm('Do you want to continue?.');\n"; sJavaScript += "if(agree)\n"; sJavaScript += "window.location = \"http://google.com\";\n"; sJavaScript += "</script>"; Response.Write(sJavaScript); } 
+1
Nov 19 '09 at 10:15
source share



All Articles