Post back and browser back button issue in asp.net
Solution 1
It's a global problem with ASP.Net. Most of developers thinks like Windows developer. It ends with a postback for mostly any action, including navigation actions.
To solve such problems:
- Avoid using button to change page. Generate an hyperlink with the target url.
- Wrap parts of the page within an update panel. The benefits is that the browser won't see a page change after each postback. Then you will be able to "refresh" the page without warning dialog
- When populating a grid, instead of "selecting" a row from codebehin, generate a link to the same page with "?ID=42" in the url, and bind this value to the grid as the selectedvalue
The root cause of this "evil" behavior, is that postback are issued using HTTP Post request. Then, the browser requires to repost the page to rebuild it.
Using the technics mentionned above, you are issuing GET request. This kind of request doesn't require to resubmit anything.
Solution 2
Try Disabling browser cache for the page that you don't want to cache.
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
Response.Cache.SetValidUntilExpires(false);
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
if (!Page.IsPostBack)
{
//CODE
}
}
user1926138
Updated on June 28, 2022Comments
-
user1926138 almost 2 years
I am working on web application. I have a grid that shows data to the user. When user click on the any row of the grid, it redirects user to another page, as we have a asp link control on the a column. Issues
My code is like
if (!Page.IsPostBack) { //CODE }
When user click on the BROWSER BACK button, it does not execute the CODE. Simply show the data from CACHE. How can I execute the CODE , on browser back button click ?
-
Casper Leon Nielsen over 9 yearsReason for downvoting: Agree on the part that we dont navigate by using postbacks. The rest is kinda not-in-tune with the whole asp.net framework idea, that it indeed [try to] function like winforms development and we do our actions in postbacks....