I have 3 textboxes, 2 dropdownlists, 2 RadGrid, 2 Poeple's picker control on a page, when user click the page's asp:button (save button), records saved to database and page redirects to another page.
But when user click back button of the browser, those data input in textboxes on previous page are still there, and If again user click on asp:button without changing the textbox values, duplicate records are saved into database.
Previous input value only persist for Textboxes not for any other control on click of back button.
Please let me know how to solve this issue. My all textbox controls are inside asp:FormView.
Thanks in advance.

- 435
- 1
- 9
- 47
-
Clear thetextbox value before redirecting to different page!! not elegant :P – too_cool Aug 06 '15 at 06:29
-
I think [this question](http://stackoverflow.com/q/2630986/4519059) or [this question](http://stackoverflow.com/q/9686224/4519059) can be helpful ;). – shA.t Oct 04 '15 at 07:02
4 Answers
Try stop browser caching. The back button doesn't always reload the page.
// Stop Caching in IE
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
// Stop Caching in Firefox
Response.Cache.SetNoStore();
Check following for detail:
-
Thank you for the reply. Please let me know where shall I put the 2 lines suggested by you? Inside Page_Load event ? – timz_123 Aug 06 '15 at 06:32
-
@user3196511 You can access your Response anywhere in your page. override the OnInit() Mehthod is one way to do it . Check out the second link http://stackoverflow.com/questions/914027/disabling-browser-caching-for-all-browsers-from-asp-net – LeY Aug 06 '15 at 07:08
-
When I try your code in override OnInit() method. I get this error page: **Confirm for Resubmission ,Error_Cache_Miss** Please reply what shall I change in code. Also, one of the textbox not get clear with this code, is it because it has `Text='<%# Bind("BarcodeNo") %>'` this ? – timz_123 Aug 06 '15 at 08:10
Modern browsers implement something known as back-forward cache (BFCache). When you hit back/forward button the actual page is not reloaded (and the scripts are never re-run).
If you have to do something in case of user hitting back/forward keys -- listen for BFCache pageshow and pagehide events.
A pseudo jQuery example:
$(window).bind("pageshow", function() {
// update hidden input field
});

- 500
- 7
- 23
-
Thankyou for the reply. Since 1 of the textbox has **auto post back = true**, so when I try your code, the textbox selected index changed event trigger and textbox becomes empty while **inserting** records. I want this functionality on browser back button click. Please reply – timz_123 Aug 06 '15 at 08:22
-
For every other Textbox your code is working fine. But only problem is with 1 Textbox which has `OnTextChanged` event and `AutoPostBack="true"`, so when I put this Textbox Id in your code, it clear the Textbox while **Inserting** the records (so I am unable to save the record of this Textbox). Please let me know solution for this one. Please reply – – timz_123 Aug 06 '15 at 08:41
-
-
-
Because for 1 Textbox I am using OnTextChanged event, thatswhy I had to use AutoPostBack = true for that Textbox. Please reply some solution – timz_123 Aug 06 '15 at 09:36
-
Are you sure that you need AutoPostBack="true" when you use OnTextChanged event? – Samane Aug 06 '15 at 10:04
-
See It helps you: In JavaScript, onbeforeunloadevent is fired when the page is about to unload and there can be multiple reasons for this unload. For instance, back or forward or refresh button is clicked or a link on the page is clicked, etc.
Normally, onbeforeunloadevent is used in order to handle browser back button functionality as follows:
<body onbeforeunload=”HandleBackFunctionality()”>
function HandleBackFunctionality()
{
if(window.event) //Internet Explorer
{
alert("Browser back button is clicked on Internet Explorer...");
}
else //Other browsers e.g. Chrome
{
alert("Browser back button is clicked on other browser...");
}
}
But there is a problem that identical event occurs once a user clicks on refresh button of a browser. So, to grasp whether or not refresh button or back button is clicked, we will use the subsequent code.
if(window.event.clientX < 40 && window.event.clientY < 0)
{
alert("Browser back button is clicked...");
}
else
{
alert("Browser refresh button is clicked...");
}
The above code snippet works well in browsers aside from FireFox. In case of FireFox, we need to apply the following check:
if(event.currentTarget.performance.navigation.type == 1)
{
alert("Browser refresh button is clicked...");
}
if(event.currentTarget.performance.navigation.type == 2)
{
alert("Browser back button is clicked...");
}
So, the consolidated code snippet will look as:
function HandleBackFunctionality()
{
if(window.event)
{
if(window.event.clientX < 40 && window.event.clientY < 0)
{
alert("Browser back button is clicked...");
}
else
{
alert("Browser refresh button is clicked...");
}
}
else
{
if(event.currentTarget.performance.navigation.type == 1)
{
alert("Browser refresh button is clicked...");
}
if(event.currentTarget.performance.navigation.type == 2)
{
alert("Browser back button is clicked...");
}
}
}

- 500
- 7
- 23
-
I could'nt get your reply. Please check my posted reply to you I am only facing issue for the Textbox that has OnTextChanged event in it. Rest your previous solution is working fine. Please look my previous reply and suggest some solution for that issue. – timz_123 Aug 06 '15 at 09:23
-
Simple. Add this html attribute to any field you don't want autofilled by the browser.
autocomplete="off"
for example:
<input type="email" name="email" autocomplete="off">

- 19,880
- 21
- 76
- 94