4

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

Victor
  • 1,108
  • 9
  • 29
  • 53

3 Answers3

14

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.

Hanlet Escaño
  • 17,114
  • 8
  • 52
  • 75
  • 1
    Incredible. Exactly what I was looking for. Thanks very much – DNR Aug 25 '12 at 15:12
  • @HanletEscaño.. this page does a submit. Is there a way to not make the page submit after the button is clicked? Thx – LearningCSharp Oct 11 '12 at 10:12
  • If you are doing this from a button, try maybe changing the submitBehavior property. It is the only thing I can think of right now. If it does not work please let me know. – Hanlet Escaño Oct 11 '12 at 14:11
  • Changing UseSubmitBehavior property did not work. I am going to create a new post and see if someone else can come up with a response. Thanks for the code, this is something I have been looking for a very long time. – LearningCSharp Oct 12 '12 at 09:48
1
 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);
Waqar Janjua
  • 6,113
  • 2
  • 26
  • 36
1
  string  strScript = "<script>"+"alert('The User has been added successfully.');";
                strScript += "window.location='AdminPanel.aspx';";
                strScript += "</script>";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "Startup", strScript);