Confirm Resubmission page on clicking back button - asp.net

In my page I have a droplist which with OnSelectedIndexChanged event, which initiates a POST request to the server on selection change.
Whenever I navigate away from the page to someother page and try to click the back button in the browser it gives me a "Confirm Resubmission Page" since it has a post data associated for the page.
I cant use redirect to clear the post data since i need the data from the droplist control and other controls to be retained even after the OnSelectedIndexChanged. Is there any solution to come around this issue??

Take a look at this question. May be it can help.
If you can't redirect to self after your post request then you can do it using ajax.

Related

hyperlink and responce.redirect

I am new to asp.net and i need to know
whats the differences between a button with response.redirect behind it
and a hyper link ? they get you both to the page but one is server side while the other is not
is it the only difference
and when it is best to use either one of them
A hyperlink render a link to the given NavigateUrl in the browser. When the user clicks on it, the browser directly goes to the destination.
A button with redirect renders a button which does a postback using javascript. When the user clicks on it, a postback is done to the same page and the browser is instructed to go to another URL using Response.Redirect.
The main difference is that the second solution loads the same page again, while the first solution goes directly to the destination. With a button you can run some code before redirecting, or redirect to a different URL based on the information in the postback. Because it loads the original page before loading the destination page, it is a bit slower.
With a hyperlink, the visitor will see where the hyperlink leads to. With a button you can not see this. A hyperlink is thus better for search engines, because they will follow a hyperlink and will not follow a button.
If you know the URL in advance and you do not want to run extra code when the user clicks on something, use a hyperlink. Else, use a button.
A hyperlink is a link which will redirect you somewhere in the same tab or in a new tab when you click on it.
In case of response.sendredirect() the user's browser is redirected to a link sent by the server depending on your business logic.(the link may belong to some other domain).
hope that helps
Hyperlink control
This is server control use for navigation to another page specified in the NavigateURL property. Hyperlink control doesn’t expose any server side event.
Response.Redirect method
This method is used to navigate to another page from code. You can use this method to navigate from a Linkbutton or ImageButton control.

Automatic page refresh even we navigate to another page

i have a problem in page navigating. please help me to solve out.
in one asp.net page forexample Page1.aspx i have dropdown when we select an item in dropdown that would display in a label. Then we navigate to another page from page1.aspx to page2.aspx by clicking linkbutton in page1.aspx. Again if i come to page1.aspx that previously selected value of dropdown should appear in label.
please help me.
HTTP is a stateless protocol. This means that it'll forget anything that you don't tell it to specifically ask it to.
When going back to Page1.aspx, your program has no idea what was selected before.
In terms of persisting user choices, you should look into storing them in Session ( or alternatively, cookies ) and checking for a pre-existing choice when returning to Page1.aspx.
If you have a saved value for the user at that point, you'll be able to set the correct value during the Page_Load event.
If it is like a wizard, I suggest you use a wizard control in the same page.
here is an example,
https://web.archive.org/web/20211020103244/https://www.4guysfromrolla.com/articles/061406-1.aspx

listview itemcommand not firing after back button

I have page that uses a multiview. Each view contains a separate user control. One of these user controls has a list view with an image button that causes the loading of a different view in the multiview. All is fine up until this point. When the user hits the back button, they are taken back to the user control that contains the list view. The user then clicks on another image button to view different data and it returns to the detail user control using the same data as before. While debugging, I have seen that the item command event does not fire after hitting the back button.
I have tried replacing the multiview and putting each user control into separate panels. This did not change the outcome at all.
I have tried setting a cookie that expires 5 seconds after page load. When the user continues to the next page, then clicks back (and it has been longer than 5 seconds), I force the form to submit again. This loads the next control again instead of reloading the page.
I have tried setting the cacheability to no cache. This causes a "page expired" message and the user has to refresh the page. This is ugly for the user and definitely takes away from the user experience.
I am looking for the cleanest way for a user to click back and have the page reloaded so that the item command event fires correctly again.
The reason is that Back doesn't affect the Page Life Cycle. It's definitely because the page is cached and cached page doesn't execute on server. You can try this code to get rid of this issue.
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
It took a lot of research to find this answer, so hopefully other people stumble upon this question and find my answer. I was astonished that I was actually able to find this. Ok, enough gloating.
Because the page does not postback when the user clicks the back button, the events are not fired correctly causing problems with the next page. What has to happen is you need to be able to handle the browser's navigation buttons (i.e. back and forward). To do this you have to set EnableHistory to true within the script manager and handle the Navigate event from the script manager. You can then reload the controls using the information you save in the state object.
I used these articles from Dino Esposito on DotNetSlackers.com as a reference. Server Side History Management and Client Side History Management

WebForms with ASP.NET AJAX: HyperLink vs LinkButton & Response.Redirect from GridView

I have 2 asp.net web forms. The first has a ScriptManager, History, UpdatePanel and GridView; the later is ScriptManager, UpdatePanel and TextBoxes. The premise here is a list that links to a detail form where an item can be edited.
From within the GridView (inside the UpdatePanel), if I use a HyperLink control with the url set to the edit page (with the necessary parameters), change something, save it and then click the back button I see the original list with no updates. Pressing F5 to refresh shows the changes.
If instead I use a LinkButton inside the GridView, and handle that LinkButton in code-behind to perform a Response.Redirect to the same edit page (with the same parameters), make the same changes, save and then click the back button, the list on the original page refreshes automatically to show my changes.
Note that the code in the detail page where the editing/saving takes place does not change - only the way it is first displayed is changed.
My question is this: what is it about the Response.Redirect that causes the page to be refreshed when the back button is clicked, and it it possible to replicate this for the direct HyperLink approach? I would prefer to use the HyperLink method as I see no reason for the postback, but I want the GridView to refresh when the user browses back to it.
Thanks.
A LinkButton causes a postback, the response to which is a HTTP 302 redirect command triggered on the server side by your Response.Redirect. Your web browser therefore does not cache the old version of the page.
The Hyperlink control simply renders a regular <a> tag which takes you to the detail page on the client side. The browser has no reason to believe the page may have changed, so it presents the cached version when you hit the back button.
If you want to tell the browser specifically not to cache the page if the back button is used,
use the cache-control HTTP header. W3C Link,
In any case, you should provide a link on the detail page (or automatic redirect on accepting changes) which takes the user back to the GridView/summary page, so they don't have to resort to using the back button.
Edit:
Sorry, the previously provided header example was not for Asp.net, but basically you'll want to do something like this:
Response.AppendHeader("Cache-Control", "no-cache")

AJAX, postbacks and browser refreshes

I have created a user control to handle adding comments to certain business entities, like contacts and customers. Works great ... except for one issue.
I am using a ListView control to edit and delete comments, and a separate area, on the same user control to add a new comment. All of this is wrapped in an UpdatePanel.
Here is my scenario ... the user adds a new comment ... the page does a postback, the data is successfully saved, and the ListView control is updated to show the new comment. Now, if the user refreshes the browser, it will naturally postback again and will add another duplicate record.
Any ideas on how best to prevent this?
You could try using the Post/Redirect/Get pattern. Basically instead of letting the postback send the data, redirect to the page. That way, if a user refreshes, s/he is refreshing the GET command rather than the POST.
Sorry.. missed the UpdatePanel piece. Make sure that your submit button is also within that UpdatePanel. A page refresh would not affect your AJAX call, but when the button is outside the panel, it's doing a regular postback so you would be sending the Add Request again.
I haven't used ASP.NET in a few years, but you should wrap your "do this on postback" code in Page.IsPostBack:
if(IsPostBack) {
//do your data-saving code...
}
MSDN link

Resources