1

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>

And here is the registration.aspx.cs file:

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

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:

enter image description here

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

Sait
  • 19,045
  • 18
  • 72
  • 99
  • The `Response.Redirect` will done the work, you probably did not apply it correctly. – Aristos Mar 30 '13 at 19:50
  • @Aristos, can you give the correct version of using `Response.Redirect` please? – Sait Mar 30 '13 at 20:08
  • @zagy If you want you can redirect the page after submit. But, I think it is common behaviour of form. You can see same behaviour of form on many site like gmail login page. – Vivek Goel Mar 30 '13 at 20:10
  • @zagy No, I prefer you write the non working code. Also see that: http://stackoverflow.com/questions/5381162/post-redirect-get-with-asp-net – Aristos Mar 30 '13 at 20:12
  • @Aristos I tried adding the line, `Response.Redirect("register.aspx")` to `RegisterUser` and it didn't work. What was your solution? PS: I read the mentioned `SO` question, i couldn't understand it. – Sait Mar 30 '13 at 20:21
  • @zagy Try to add a random url parameter at the end to force to load it again and not get it from the cache. – Aristos Mar 30 '13 at 20:40
  • @Aristos I don't want to make my url looks dirty. Is there any other way of doing it? – Sait Mar 30 '13 at 20:44
  • @zagy Disable the client cache by adding headers. – Aristos Mar 30 '13 at 20:49
  • @Aristos I disabled client cache using `Response.Cache.SetCacheability(HttpCacheability.NoCache);` still have the same problem. – Sait Mar 30 '13 at 22:04

2 Answers2

0

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

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.

reaz
  • 735
  • 1
  • 10
  • 20
  • Yes but we use SSL for the page (RegisteringUsers.aspx). because it connects to database. – reaz Mar 30 '13 at 20:24
  • and if you want to create more secure session. that's another story.. you need to use (SessionFacade) class. – reaz Mar 30 '13 at 20:25
  • So, basically, you actually use another `.aspx` file rather than using just another function like my `RegisterUser()` function to prevent this error message? – Sait Mar 30 '13 at 20:28
  • Let's think about it in this way... If you fill the information and submitting it in the same page... you will probably get the resend message if someoen refresh the page. but what we do is that after submitting the information...the user will be directed to another page (where the RegisterUser()) function is in...and again redirected to the Register.aspx page or anyother pages. – reaz Mar 30 '13 at 20:30
  • I will try to write a simple code for you. just give me 5 min – reaz Mar 30 '13 at 20:31
  • That would be wonderful, I was still trying to understand it. – Sait Mar 30 '13 at 20:39
  • I don't think I need to use `UpdatePanel`. It may reduce the performance. – Sait Mar 30 '13 at 22:02
  • UpdatePanel will not change anything. it's just a partial update on your page. – reaz Mar 30 '13 at 22:04
  • I have no idea about that. Visual studio 2012 is the latest version when you wopen a new website template. they used UpdatePanel in every webpages by default... – reaz Mar 30 '13 at 22:10
0

please set causesvalidation="false" in your TextBox and button control

for try below code

<asp:TextBox ID="txtUserName" CausesValidation="false" runat="server"></asp:TextBox>
        <asp:Button ID="btnRegister"  CausesValidation="false" runat="server" Text="Button" onclick="RegisterUser" />
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234