Skip to content Skip to sidebar Skip to footer

Asp.net Webform Display Alert And Redirect

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 th

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:

protectedvirtualvoidDisplayAlert(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:

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.

protectedvoidSave(..){   
    // 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):

protectedvoidPage_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 3:

This works perfect:

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

Solution 4:

protectedvoidSave(..)
{       
    // Do save stuff    
    ShowMessageBox();  
} 

privatevoidShowMessageBox()
{        
    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);
}  

Post a Comment for "Asp.net Webform Display Alert And Redirect"