阻止ASP.Net中的表单重新提交(不重定向到我自己) [英] Prevent Form Resubmission in ASP.Net (without redirecting to myself)

查看:159
本文介绍了阻止ASP.Net中的表单重新提交(不重定向到我自己)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有Form元素的母版页(< form runat =server> ),一个带有Button元素的内容页面(< asp:Button ID =Button1runat =serverOnClick =Button1_ClickText =Sync/> ),以及包含<$ c的Code Behind页面$ c> Button1_Click function。

I have a master page with a Form element (<form runat="server">), a content page with a Button element (<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Sync" />), and a Code Behind page that contains the Button1_Click function.

用户进入页面并单击按钮。 Code Behind代码执行(在服务器上),在其中更新数据库中的某些表。代码隐藏代码的最后一件事是在内容页面上设置Span元素的 InnerHTML ,并显示成功或失败消息。

The user comes to the page and clicks the button. The Code Behind code executes (on the server) in which it updates some tables in a database. The last thing the Code Behind code does is to set the InnerHTML of a Span element on the content page with a success or failure message.

一切都很好。问题是如果用户刷新页面,则重新提交表单,浏览器会询问是否真的是用户想要的。如果用户回答是肯定的,则重新执行Code Behind代码并再次更新数据库。如果用户做出负面回应,那么没有任何反应。

That all works great. The problem is if the user refreshes the page, the Form is resubmitted and the browser asks if that's really what the user wants. If the user responds in the affirmative then the Code Behind code is re-executed and the database is updated again. If the user responds negatively, then nothing happens.

重新执行Code Behind代码并不是什么大问题。它不会伤害任何东西。但这并不是我想要的行为。

Re-executing the Code Behind code is not a big deal. It won't hurt anything. But it's not really the behavior I want.

我知道我可以使用Response.Redirect()重定向回页面,但用户从未看到我的成功或失败的消息。我应该提一下,消息实际上不仅仅是成功或失败,否则我想我可以在重定向上向QueryString添加一些东西。

I know I can do a redirect back to the page with Response.Redirect() but then the user never sees my success or failure message. I should mention that the message is really more than just "Success" or "Failure" otherwise I suppose I could add something to the QueryString on the Redirect.

是否有从代码隐藏代码重置Form元素的方法,以便如果用户刷新页面不重新提交表单?

Is there a way to reset the Form element from the Code Behind code so that if the user refreshes the page the Form is not resubmitted?

母版页......

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="IntuitSync.SiteMaster" %>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" xmlns:ipp="">
    <head runat="server">
    </head>
    <body>
        <form runat="server">
            <div runat="server" id="mainContetntDiv">
                <asp:ContentPlaceHolder ID="MainContent" runat="server" />
            </div>
        </form>
    </body>
</html>

内容......

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Site.master" CodeBehind="SyncToCloud.aspx.cs" Inherits="IntuitSync.SyncToCloud" %>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Sync" />
    <span runat="server" id="SyncStatus"></span>
</asp:Content>

守则背后......

The Code Behind...

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Web;

namespace IntuitSync
{
    public partial class SyncToCloud : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            /*
                Do a bunch of stuff and put the results in syncResults which is a List.
            */
            SyncStatus.InnerHtml = string.Join("", syncResults.ToArray()); // I'd rather do this...
            //Response.Redirect(Request.Url.PathAndQuery, true);  // ...and not do this.
        }
    }
}

任何和所有帮助都很大赞。

Any and all help is greatly appreciated.

推荐答案

问题是你正在做一个表格POST,如果用户随后刷新页面,那么就做了什么应该这样做:重新提交POST。没有其他办法了。因此,您可以选择的选项如下:

The issue is you're doing a form POST which, if the user subsequently refreshes the page, does what it's supposed to do: resubmit the POST. There is no way around it. Therefore, the options available to you are as follows:


  1. 重定向到确认页面,以某种方式将操作结果传递给确认页面(通常从某些数据存储库重新加载,如果它对查询字符串/ cookie /等不可行。)。

  2. 重定向到同一页面,但通过查询字符串传递成功消息参数(或愚蠢并将其存储在会话/ ViewState /你有什么愚蠢)。

  3. 而不是张贴,做一个表格,因为它似乎你没有张贴任何值,只启动某种服务器端处理。要做到这一点,只需将表单从帖子更改为获取,或只是做一个链接。这里的危险是你不应该在任何获取操作中进行任何变形/破坏性操作。

你可能最开心最后一个选项,但这一切都取决于做一堆东西并将结果放在syncResults中......真的需要。

You probably would be happiest with the last option, but it all depends on what "do a bunch of stuff and put the results in syncResults..." really entails.

这篇关于阻止ASP.Net中的表单重新提交(不重定向到我自己)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆