在 Response.Redirect 之后调用函数

发布于 2024-09-15 08:17:22 字数 609 浏览 1 评论 0原文

我有一个 ASP.NET 站点,我正在使用 jGrowl 向用户显示通知。当用户停留在同一页面上时,这非常有用,但是如果我重定向用户怎么办?

例如,AddPerson.aspx 是用于添加新人员的页面。添加人员后,用户将重定向到 People.aspx。然后我想显示通知“乔什·史密斯已被添加。”。

目前,我的母版页中有一个执行 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);
}

如何在 Response.Redirect 之后调用它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

活泼老夫 2024-09-22 08:17:22

Response.Redirect 结束页面上的处理(并引发 ThreadAbort 异常)。您无法在 Response.Redirect 之后调用。之前调用它,或者将其作为您重定向到的页面中的第一个内容。

文档说您可以设置该值将 endResponse 参数设置为 false,并继续执行代码,但在实践中,我发现它有时有效,而其他则无效。这是我不信任文档的罕见情况之一,实际上,事后重定向通常是我想要的。

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.

瞎闹 2024-09-22 08:17:22

简短的回答是你不能。重定向后页面不再执行。

更长的答案是,如果您在重定向之前设置了 cookie 或会话变量,您可以在 people.aspx 页面中检测到它,并在设置时调用您的函数(不要忘记清除就如这一点)。

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).

琉璃繁缕 2024-09-22 08:17:22
Response.Redirect(newUri, false);
//stuff needed to be done before ending the handling here.
ApplicationInstance.CompleteRequest();

但通常情况下,我使用自己的重定向,这样我可以更清楚地知道我想要 301、303、307 还是(很少)302 Response.Redirect。

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.

柠檬色的秋千 2024-09-22 08:17:22

您必须选择的一个选项是“通知”页面最近添加了用户。例如,您可以在 URL 中放置一些内容来表示这一点,或者您可以在会话中放置一些内容。然后,当您呈现 People.aspx 时,检查该值以查看是否刚刚添加了某人,如果是,则显示通知。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文