Question

I am relatively new in programming and I have a doubt about postback. I searched in some pages, but I couldn't solve my question.

I created a web page and I use postback in it that updates some informations in the page. That works without any problems. However, after the postback is fired, if the user presses F5, a message appears asking to confirm the form re-submit.

Why exactly this message appears and how can I avoid it? If the user would press F5, I want the page reload, without any alerts.

I'm sorry if my question wasn't clear enough, but I really don't understand postback how much I'd like.. =)

Was it helpful?

Solution 4

I thought in the following code to solve my problem, using a javascript event:

$(document).keydown(function (event) {
    if (event.keyCode == 116) {
        location.href = 'pedidos.aspx';
        return false;
    }
});

It solves my problem, because I avoid the postback message as I want and the page is reloaded. But is this a good solution?

OTHER TIPS

By Default ,

The method is post means it will confirm while reloading the page.

The method is get means it wont ask anything.

Why this happens,Whenever the post method called severe action gonna happen in server,so it just confirms from the user.There is no need in the case of get Method.

I hope u are clear with this solution.

One way to avoid your issue is to place a hidden field on the page. When the form is submitted, check the hidden field. If no value, process the form, and populate the hidden field with a value. If it has a value, then do nothing.

I prefer using an on-click event on a button, instead of using the postback event.

As to why it happens, a refresh sends a request along with the form data back to the server causing another postback.

This is known as page re-submission.

When you refresh the browser, it will resend the last request you did. If it was a POST request (like you do in case of postback) then it will re-post the information but before doing it you'll see the warning message you describe.

To prevent this:

Page.Response.Redirect(Page.Request.Url.ToString(), true);

Which changes the response to a GET instead of a POST. Prevents the 'Form Resubmission' dialog.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top