使用 ASP.NET AJAX 的 WebForms:HyperLink 与 LinkBut​​ton 和 LinkBut​​ton来自 GridView 的 Response.Redirect

发布于 2025-01-01 03:19:39 字数 609 浏览 2 评论 0原文

我有 2 个 ASP.NET Web 表单。第一个有 ScriptManager、History、UpdatePanel 和 GridView;后者是ScriptManager、UpdatePanel 和TextBoxes。这里的前提是一个链接到可以编辑项目的详细信息表单的列表。

从 GridView 内部(在 UpdatePanel 内部),如果我使用超链接控件,并将 url 设置为编辑页面(带有必要的参数),请更改某些内容,保存它,然后单击后退按钮,我会看到没有更新的原始列表。按 F5 刷新即可显示更改。

相反,如果我在 GridView 中使用 LinkBut​​ton,并在代码隐藏中处理该 LinkBut​​ton 以执行 Response.Redirect 到相同的编辑页面(具有相同的参数),进行相同的更改,保存,然后单击后退按钮,原始页面上的列表会自动刷新以显示我的更改。

请注意,进行编辑/保存的详细信息页面中的代码不会更改 - 仅更改其首次显示的方式。

我的问题是:Response.Redirect 是什么导致单击后退按钮时刷新页面,并且可以为直接超链接方法复制此内容?我更喜欢使用 HyperLink 方法,因为我认为没有理由进行回发,但我希望当用户浏览回 GridView 时刷新它。

谢谢。

I have 2 asp.net web forms. The first has a ScriptManager, History, UpdatePanel and GridView; the later is ScriptManager, UpdatePanel and TextBoxes. The premise here is a list that links to a detail form where an item can be edited.

From within the GridView (inside the UpdatePanel), if I use a HyperLink control with the url set to the edit page (with the necessary parameters), change something, save it and then click the back button I see the original list with no updates. Pressing F5 to refresh shows the changes.

If instead I use a LinkButton inside the GridView, and handle that LinkButton in code-behind to perform a Response.Redirect to the same edit page (with the same parameters), make the same changes, save and then click the back button, the list on the original page refreshes automatically to show my changes.

Note that the code in the detail page where the editing/saving takes place does not change - only the way it is first displayed is changed.

My question is this: what is it about the Response.Redirect that causes the page to be refreshed when the back button is clicked, and it it possible to replicate this for the direct HyperLink approach? I would prefer to use the HyperLink method as I see no reason for the postback, but I want the GridView to refresh when the user browses back to it.

Thanks.

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

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

发布评论

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

评论(1

猥︴琐丶欲为 2025-01-08 03:19:39

LinkBut​​ton 会导致回发,其响应是由 Response.Redirect 在服务器端触发的 HTTP 302 重定向命令。因此,您的网络浏览器不会缓存旧版本的页面。

超链接控件仅呈现一个常规 标记,该标记将您带到客户端的详细信息页面。浏览器没有理由相信页面可能已更改,因此当您点击后退按钮时,它会显示缓存的版本。

如果您想明确告诉浏览器在使用后退按钮时不要缓存页面,
使用缓存控制 HTTP 标头。 W3C 链接

在任何在这种情况下,您应该在详细信息页面上提供一个链接(或在接受更改时自动重定向),该链接将用户带回 GridView/摘要页面,这样他们就不必求助使用后退按钮。

编辑:

抱歉,之前提供的标头示例不适用于 Asp.net,但基本上您需要执行以下操作:

Response.AppendHeader("Cache-Control", "no-cache")

A LinkButton causes a postback, the response to which is a HTTP 302 redirect command triggered on the server side by your Response.Redirect. Your web browser therefore does not cache the old version of the page.

The Hyperlink control simply renders a regular <a> tag which takes you to the detail page on the client side. The browser has no reason to believe the page may have changed, so it presents the cached version when you hit the back button.

If you want to tell the browser specifically not to cache the page if the back button is used,
use the cache-control HTTP header. W3C Link,

In any case, you should provide a link on the detail page (or automatic redirect on accepting changes) which takes the user back to the GridView/summary page, so they don't have to resort to using the back button.

Edit:

Sorry, the previously provided header example was not for Asp.net, but basically you'll want to do something like this:

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