views:

55

answers:

4

I have an ASP.NET site and I'm using jGrowl to display notifications to the user. This works great when the user stays on the same page, but what if I'm redirecting the user.

Example, AddPerson.aspx is a page for adding a new person. After a person is added the user is redirect to People.aspx. I'd like to then display the notification 'Josh Smith has been added.'.

I currently have a function in my Master page executing the javascript:

public void DisplayNotification(string html, string header)
{
    string text = "\"" + html + "\"";
    string options = "{ header: '" + header + "', life: 6000 }";

    string scriptJGrowl = "$.jGrowl(" + text + ", " + options + ");";
    ScriptManager.RegisterStartupScript(Page, GetType(), Guid.NewGuid().ToString(), scriptJGrowl, true);
}

How can I call this after a Response.Redirect?

+3  A: 

Response.Redirect ends processing on the page (and throws a ThreadAbort exception). You can't call after Response.Redirect. Call it before, or have it be the first thing in the page you're redirecting to.

The documentation says that you can set the value of the endResponse parameter to false, and have code execution continue, but in practice, I've seen it work sometimes, and not work others. THis is one of those rare cases where I don't trust the documentaiton, and really, redirecting afterward is usually what I want anyway.

David Stratton
A: 

The short answer is that you can't. After the redirect the page no longer executes.

The longer answer is that if you set a cookie or a session variable before the redirect, you can detect that in your people.aspx page and call your function if it is set (don't forget to clear it as this point).

Oded
A: 
Response.Redirect(newUri, false);
//stuff needed to be done before ending the handling here.
ApplicationInstance.CompleteRequest();

Normally though, I use my own redirect that lets me be clearer whether I want a 301, 303, 307 or (rarely) the 302 Response.Redirect does.

Jon Hanna
A: 

One option that you have to is to "inform" the page that a user has recently been added. For example you can place something in the URL to signal this, or you can place something in the session. Then when you render People.aspx check that value to see if someone has just been added, if so then show the notification.

Sayed Ibrahim Hashimi