Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

showing ClientScript Alerts before Redirecting to another page in ASP.NET c#?

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

like image 308
Victor Avatar asked Dec 05 '22 14:12

Victor


1 Answers

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.

like image 115
Hanlet Escaño Avatar answered May 16 '23 07:05

Hanlet Escaño