0

When I do a postback, why the browser always ask me a confirmation before refreshing ? (F5 / CTRL + F5)

My code is really simple (I use a master page) :

.aspx

...
<asp:button runat="server" ClientIDMode="static" type="button" id="btnlogin" 
                  OnClientClick="return veriflogin();" class="btn btn-primary" Text="Valider" 
                  onclick="btnlogin_Click"></asp:button>

<asp:Label ID="ok" runat="server" Visible="false" Text="allright"></asp:Label>
...

.cs

protected void btnlogin_Click(object sender, EventArgs e)
{
    ok.Visible = true;
}

The confirmation (IE):

enter image description here

Can I avoid this confirmation ?

3
  • When you refresh that page, you execute the Postback again. This message warns about potential problems with that. See also Post/Redirect/Get May 14, 2014 at 14:25
  • the browser does that automatically, it's normal behavior May 14, 2014 at 14:26
  • You can't avoid this confirmation because its managed by your browser itself. Like Jonesy said its normal behavior.
    – Mivaweb
    May 14, 2014 at 14:28

2 Answers 2

1

Postback occurs (as name suggests) using POST HTTP verb. POST by definition is not idempotent, and thus repeating the same request is not necessarily safe. In other words, repeating POST may result in different end result than doing it once. Hence the browser warns you that in order to retrieve the page again, it has to repeat POST operation which may have unintended consequences.

For example, in RESTful applications, POST implies "create" (as in CRUD, akin to SQL's INSERT) a resource. Executing POST twice means two instances of the resource will be created rather than one. Compare it with GET which is a read operation, and reading the same resource more than once does not affect the state of the resource(s), and is thus considered safe (because GET is idempotent)

6
  • So can I make my button click to use GET method to avoid this ?
    – Vinc 웃
    May 14, 2014 at 14:35
  • @VincP. buttons that calls back to the server are per definition postbacks. You should probably rethink how you want to authenticate the user. May 14, 2014 at 14:40
  • @VincP. IIRC (been awhile), ASP.NET WebForms uses one single form and any action that causes callback to server must submit the form (so all the form state is passed in with the request) and the form is POST. If instead of rendering the form back, you can do .Redirect (which browser follows up on with a GET), then this should remove that warning (but there are implications since viewstate is lost). But I hope others can lend a better suggestion as it's been a long while ago since I did WebForms.
    – LB2
    May 14, 2014 at 14:41
  • Ty, I agree, postback = post. I forgot that. Now it is clear. Ty for the suggestion. But I was wondering if I could do something, but it look like I can't ! (With this method)
    – Vinc 웃
    May 14, 2014 at 14:43
  • @VincP. This is a login form, right? After user is authenticated, how do you transfer user to the next page? Can you transition via .Redirect? If so, this may do it for you.
    – LB2
    May 14, 2014 at 14:45
0

No, you can't avoid the confirmation.

If your last action on a site was a POST, most browsers will ask for a confirmation on whether you want to refresh the site with the same POST again. That's intended behavior.

POST requests are intended to modify state in one way or another. That's why your browser warns you. GET requests should not modify state, and are therefore considered "safe" to resubmit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.