Gridview,还有那个讨厌的浏览器后退按钮

发布于 2024-12-14 01:08:51 字数 365 浏览 4 评论 0原文

我们的 ASP.NET 页面上有一个包含一些数据的 GridView,并且一列有用于选择行的复选框。 GridView 下方有一个“删除”按钮。当最终用户通过复选框选择某些项目然后单击“删除”按钮时,将触发回发并删除所选项目。很常见的东西。

我分配的问题也很常见。如果最终用户在结果页面上单击浏览器后退按钮,则(在出现始终被立即忽略的警告之后)回发将重新启动。

这里的问题是,由于所选项目已被删除,浏览器只需选择现在占据已删除项目曾经所在空间的任何行,然后继续删除这些项目。这是一件坏事™。

到目前为止,我一直在研究一些基于设置和检查会话变量的想法,以尝试跟踪回发是否发生,但我对结果还不太满意。

有没有人有一个好的(希望是简单的)方法来处理这个问题?

Our ASP.NET page has a GridView on it with some data, and one column has CheckBoxes in order to select rows. A Delete button exists below the GridView. When the end user selects some items via the CheckBoxes and then clicks the Delete button, a postback fires, and deletes the selected items. Pretty common stuff.

The problem I've been assigned is also common. If, on the resulting page, the end user clicks the browser back-button, then (after a warning which is always summarily disregarded) the post-back re-initiates.

The problem here is that, since the selected items have already been deleted, the browser simply selects whatever rows now occupy the space where the deleted item once was, and proceeds to delete those items instead. This a Bad Thing™.

So far I've been banging around some ideas based around setting and checking Session variables to try and track if a postback has occurred, but I'm not too happy with the results yet.

Does anyone have a good (and hopefully simple) way of dealing with this issue?

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

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

发布评论

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

评论(3

泪之魂 2024-12-21 01:08:51

您还可以在第一个 !IsPostback 上将一些值与会话一起放入视图状态中,然后将其更改为 IsPostback 中的其他内容。

这可能比 cookies 或浏览器缓存过期的东西更容易。

在这种情况下,如果用户单击后退按钮并提交任何内容,您可以查看视图状态并查看它是否与会话中的值匹配,如果不匹配,则永久驱逐该用户。

显然,这是假设您正在使用启用了视图状态的传统 asp.net。

希望这有帮助。

You could also put some value into viewstate along with session on the first !IsPostback and then change it to something else in the IsPostback.

This might be easier than cookies or browser cache expiration stuff.

In this case, if the user clicks the back button and submits anything, you can look at viewstate and see of it matches the value in session, if not then banish the user for eternity.

This is obviously assuming that you are using traditional asp.net with viewstate enabled.

Hope this helps.

梦在深巷 2024-12-21 01:08:51

我认为没有简单的方法。

您可以使用 javascript 并尝试各种 http 浏览器缓存设置,但您不能真正依赖其中任何一个。您必须假设用户能够重新发布表单。

我认为你在服务器代码中处理它是正确的。怎么样:

在网格页面的 GET 中创建一个 cookie/session 变量。
使网格页面的 POST 中的 cookie/会话变量无效。

这样,当通过后退按钮重新发布时,您的代码应该检测到没有有效的 cookie/会话值,然后可以重新发出到同一页面的重定向,强制执行 GET。

PRG 模式。

当您提到回发中的“结果页面”时,听起来您已经在使用 Post-Redirect-Get 模式。如果您不这样做,那么这有助于解决用户在发布的表单上按 F5 的其他问题。

祝你好运!

I don't think there is an easy way.

You can play around with javascript and try various http browser cache settings but you can't really rely on any of it. You have to assume a user is going to be able to re-post the form.

I think you're right to deal with it in server code. How about this :

Create a cookie/session variable in the GET of the grid page.
Invalidate that cookie/session variable in the POST of the grid page.

This way when re-POSTed via the back button, your code should detect there's no valid cookie/session value and could then re-issue a redirect to the same page forcing a GET.

PRG Pattern.

It sounds like you are already using a Post-Redirect-Get pattern as you mention "a resulting page" from the postback. If you're not then this helps with the other problem of users pressing F5 on a posted form.

Good luck!

蘸点软妹酱 2024-12-21 01:08:51

异步删除 GridView 中的记录怎么样? (使用 Updatepanel)

这将阻止浏览器将选定的复选框保存在浏览器缓存中,并消除用户在完全回发后刷新页面时烦人的安全弹出浏览器检查。

或者,您是否尝试过在第一次绑定后取消选中代码隐藏中的所有复选框? (假设在 Page_Load 上)

尝试这个:

<asp:UpdatePanel id="upGv" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:GridView ID="gv" runat="server" />
        <asp:Button ID="btnDelete" runat="server" Text="Delete" />
    </ContentTemplate>
</asp:UpdatePanel>

在代码隐藏中:

Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
    ' do a for loop searching for selected checkboxes and do the deleting here
    ' --- delete codes ---
    ' rebind the data in gridview
    gv.DataSource = 'some dataset/datatable etc etc
    gv.DataBind()
    'update the updatepanel
    upGv.Update()
End Sub

How about doing the deletion of records in GridView Asynchronously? (using an Updatepanel)

This will prevent the browser to save the selected checkboxes in Browser Cache and removes the annoying security popup browser checking whenever the user refreshes the page after a Full Postback.

Or by any chance, have you tried unchecking all the checkboxes in code-behind after the first binding? (let's say on Page_Load)

try this:

<asp:UpdatePanel id="upGv" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:GridView ID="gv" runat="server" />
        <asp:Button ID="btnDelete" runat="server" Text="Delete" />
    </ContentTemplate>
</asp:UpdatePanel>

in code-behind:

Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
    ' do a for loop searching for selected checkboxes and do the deleting here
    ' --- delete codes ---
    ' rebind the data in gridview
    gv.DataSource = 'some dataset/datatable etc etc
    gv.DataBind()
    'update the updatepanel
    upGv.Update()
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文