问题
I gotta address a problem here...
I'm developing a system where I am constantly showing Messages to the user, For instance... I'm adding a new client, I input all the data and press Save, ON C# what I do is do all the process of saving, then I use...
ClientScript.RegisterStartupScript(this.GetType(), "Mensaje", "alert('Client Saved Successfully');", true);
And that's it... Eventually the message is displayed since I'm staying in the same site (because if I want to add another client, I must stay in the sme site, not send me back to my MAIN SCREEN) or at least I'm not telling the system to do a redirection...
However here's another example...
If I'm going to edit a client, I'm redirected to the edit screen, I input all my changes and click SAVe, on C# I do all the process to save changes, then I use a clientscript as the one above, and then ...
this.Response.Redirect("~/Main.aspx");
However, I noticed that if I use a Response.Redirect I will not be in the same page anymore, the alert won't be displayed to the user....
This is a constant issue that I'm running into, and I would like to know what could be the solution to that...
Thank you and I hope you can help me
回答1:
This is what I have for my projects:
public static void jsAlertAndRedirect(System.Web.UI.Page instance, string Message, string url)
{
instance.Response.Write(@"<script language='javascript'>alert('" + Message + "');document.location.href='" + url + "'; </script>");
}
I use it like this from a page:
HTMLHelper.jsAlertAndRedirect(this, "This is an alert.", ResolveUrl("~/Default.aspx"));
Good luck.
回答2:
string strScript = "<script>"+"alert('The User has been added successfully.');";
strScript += "window.location='AdminPanel.aspx';";
strScript += "</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "Startup", strScript);
回答3:
string strScript = "<script >alert('The User has been added successfully.'); window.location='AdminPanel.aspx';< /script >";
Page.ClientScript.RegisterStartupScript(this.GetType(), "Startup",strScript);
来源:https://stackoverflow.com/questions/11993048/showing-clientscript-alerts-before-redirecting-to-another-page-in-asp-net-c