0

Everywhere I read questions about how to remember it, but I want to reset it!

I use this code:

<table>
    <tr>
        <td>Your name:</td>
        <td><asp:TextBox runat="server" ID="user_name" MaxLength="20" AutoCompleteType="None" /></td>
    </tr>
    <tr>
        <td><table><tr><td>Captcha:</td><td><img src="turing.aspx" alt="Enable images to see captcha image" /></td></tr></table></td>
        <td><asp:TextBox runat="server" ID="txt_captcha" MaxLength="5" AutoCompleteType="None" /></td>
    </tr>
    <tr>
        <td>Email for new messages reports:</td>
        <td><asp:TextBox runat="server" ID="txt_eml" MaxLength="100" AutoCompleteType="None" /> (*this is not required)</td>
    </tr>
    <tr>
        <td>Message text:</td>
        <td><asp:TextBox runat="server" ID="comment_text" Columns = "30" Rows = "5" Wrap = "true" TextMode="MultiLine" AutoCompleteType="None"/></td>
    </tr>
    <tr>
        <td colspan = "2">
            <asp:Button runat="server" ID="Button1" Text="Add message" OnClick="HandleAddMessage" CausesValidation="true" ValidationGroup="second_group" />
        </td>
    </tr>
</table>

At HandleAddMessage I even reset those manually that way: user_name.Text = txt_eml.Text = txt_captcha.Text = comment_text.Text = string.Empty;

But after message added, if I refresh the page old values comes back. What I can do to prevent it?

Kosmo零
  • 4,001
  • 9
  • 45
  • 88
  • I suppose that you actually want is that the browser would not automatically fill those values. See [Disabling Chrome Autofill](https://stackoverflow.com/questions/15738259/disabling-chrome-autofill) for more info. – laika Oct 20 '17 at 15:45
  • But how I can add this `autocomplete="off"` for asp:TextBox filed? This `autocomplete` is pure HTML tag attribute. – Kosmo零 Oct 20 '17 at 15:52
  • By "reset", you could to it in a cancel method handler on the server side. Is this what you're trying to accomplish? – IrishChieftain Oct 20 '17 at 15:55
  • @IrishChieftain - I don't understand your question. Currently user can enter some text in fields and press button. Text adds into database and text fields becomes empty, BUT if user hit refresh button in browser all text fields is filled again and even more worth it tries to add already added text, like second button press happened. – Kosmo零 Oct 20 '17 at 16:00
  • What browser are you using and can we see your handler code? – IrishChieftain Oct 20 '17 at 16:10
  • @IrishChieftain - I use IE11 and Opera 12.18. `Riv` already posted ultimate good solution, so handler code will not change anything. – Kosmo零 Oct 20 '17 at 16:14
  • I just upvoted Riv's answer. Since we don't know what other code is in your handler, be careful to choose the correct overload of the Redirect method. https://stackoverflow.com/questions/6707465/when-should-i-use-response-redirecturl-true – IrishChieftain Oct 20 '17 at 16:29

1 Answers1

2

Once the user has submitted the form the first time, just response.redirect to the same page again. It effectively resets the page, so if they do refresh it, they won't post back the same data. It would also mean that your fields will all be reset since the page has effectively reloaded.

Riv
  • 1,849
  • 1
  • 16
  • 16