Asp.net Webform Display Alert and redirect

22,819

Solution 1

You can't do a Response.Redirect because your javascript alert will never get displayed. Better to have your javascript code actually do a windows.location.href='default.aspx' to do the redirection after the alert is displayed. Something like this:

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);
}

Solution 2

This works perfect:

string url = "home.aspx";
ClientScript.RegisterStartupScript(this.GetType(), "callfunction", "alert('Saved Sucessfully.');window.location.href = '" + url + "';",true);

Solution 3

The DisplayAlert method adds the client script to the currently executing page request. When you call Response.Redirect, ASP.NET issues a HTTP 301 redirect to the browser, therefore starting a new page request where the registered client script no longer exists.

Since your code is executing on the server-side, there is no way to display the alert client-side and perform the redirect.

Also, displaying a JavaScript alert box can be confusing to a user's mental workflow, an inline message would be much more preferable. Perhaps you could add the message to the Session and display this on the Default.aspx page request.

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

Then in Default.aspx.cs code behind (or a common base page class so this can happen on any page, or even the master 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 isn't tested but should point you in the right direction

Solution 4

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);
}  
Share:
22,819
AlteredConcept
Author by

AlteredConcept

Updated on February 09, 2020

Comments

  • AlteredConcept
    AlteredConcept over 4 years

    I'm currently stuck. I have a webform with a button that registers or saves a record. What i'd like to is have it display a javascript alert and then redirect to a page. Here is the code i am using

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

    This code just redirects without giving the prompt 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 anyone help me find a solution to this?

    Thanks

  • Tony L.
    Tony L. about 8 years
    If you are doing this from within a control, you may need to add Page. in front of ClientScript like so: Page.ClientScript...