重定向后为双提交prevention GET解决方案 [英] Post redirect get solution for the double submit prevention

查看:111
本文介绍了重定向后为双提交prevention GET解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 register.aspx 这需要从用户,并将其存储在数据库中的一些信息。但是,当用户刷新页面我解决不了的双提交问题。这里是我的 registration.aspx 文件:

I have a register.aspx which takes some information from user and stores it in the database. However, I cannot solve the double submit problem when user refreshes the page. Here is my registration.aspx file:

<form id="form1" runat="server">
     <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>

     <asp:Button ID="btnRegister" runat="server" Text="Button" onclick="RegisterUser" />

     <asp:Label ID="lblErrorMessage" runat="server" Text=""></asp:Label>
</form>

这里是 registration.aspx.cs 文件:

protected void RegisterUser(object sender, EventArgs e)
{
    if (txtUserName.Text.Length < 3)
    {
        lblErrorMessage.Text = "username should be minimum 3 characters, try again.";
    }
}

不过,我尝试使用我的浏览器,Chrome浏览器测试。当我用文字一为用户名,它进入 RegisterUser 函数,并显示错误消息。但可悲的是,当我尝试刷新它要求重新提交,而我期待没有任何问题刷新页面:

However, I try to test it using my browser, chrome. When I use the text "a" for the username, it goes into the RegisterUser function and shows the error message. But sadly, when I try to refresh the page it asks for resubmission while I was expecting to refresh without any problem:

我试着用的Response.Redirect ,它也不能工作。

I tried using Response.Redirect and it didn't work either.

推荐答案

我尝试使用的UpdatePanel和模板给你一个简单的方法:

I tried an easy way for you using UpdatePanel and Template:

改变code在你的主页是这样的:

change the code in your main page like this:

<form id="form1" runat="server" >

  <asp:UpdatePanel runat="server"><ContentTemplate>
     <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
     <asp:Button ID="btnRegister" runat="server" Text="Button" onclick="RegisterUser" />
     <asp:Label ID="lblErrorMessage" runat="server" Text=""></asp:Label>
     <asp:ScriptManager ID="ScriptManager1" runat="server">
     </asp:ScriptManager>
     </ContentTemplate>
  </asp:UpdatePanel>
</form>

我认为这将在一个简单的方法解决问题。
我们如果您有任何问题,这我知道。

I think that will solve your problem in an easier way. Let me know if you have any problem with it.

这篇关于重定向后为双提交prevention GET解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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