showing ClientScript Alerts before Redirecting to

2019-04-10 07:45发布

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

3条回答
在下西门庆
2楼-- · 2019-04-10 08:10

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.

查看更多
Deceive 欺骗
3楼-- · 2019-04-10 08:10
 string strScript = "&lt;script &gt;alert('The User has been added successfully.');                 window.location='AdminPanel.aspx';&lt; /script &gt;";
 Page.ClientScript.RegisterStartupScript(this.GetType(), "Startup",strScript);
查看更多
迷人小祖宗
4楼-- · 2019-04-10 08:28
  string  strScript = "<script>"+"alert('The User has been added successfully.');";
                strScript += "window.location='AdminPanel.aspx';";
                strScript += "</script>";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "Startup", strScript);
查看更多
登录 后发表回答