Stop ASP.NET PostBacks Showing As Separate Pages?

2019-07-24 17:34发布

问题:

I have an ASP.NET form that the user can make lots of changes to, each time they make a change the page PostsBack and the details are updated.

If the user hits the browser back button they go back through all the previous versions of the page.

Is it possible to stop each PostBack being treated by the browser as a new page?

So the would make any changes they like and if they hit the back button it brings them to the previous form and not the same form but a different version?

I know I could use AJAX to update values but I'm not an advanced coder so trying to keep things simple as I haven't used AJAX before.

回答1:

You could use a trick to do it.

On postback you can set a session bit to true saying they submitted that form. On your postback check to see if that value is set. If it is they are trying to do it again and you can just abort it. It wouldn't prevent the postback per se but you could control the logic and prevent it from DOING anything.

I personally would explore ajax as Jquery provides some nice ways to do it and it'd be a learning experience but I suppose this would work as you are asking. On a per session basis. If you only want 1 submission ever use a database to store the activity.



回答2:

Ajax is your only solution.

There is no way to remove a page from the browser history. Javascript is explicitly denied the capability.

Now, you could, potentially, stop them from using the back button at all. Although this might result in unhappy users and I'm not 100% certain it works in all browsers.

function body_onload(){
    window.history.forward(1);
} 


回答3:

You could use UpdatePanel: http://msdn.microsoft.com/en-us/library/bb386454.aspx