How to Prevent Submitting Data during Refresh - c#

as i am developing a web application in asp.net, There is 4 text boxes to provide user deatils and a button to save the information in DB. When User click the button to Insert the user deatil, it is saved in DB perfectly, but the problem is when the user Refreshes the page, that data is again re inserted / resubmitted, i want to make sure that duplicate submission should be avoided .
Please any suggestion, code samples, to avoid this situation are welcome ( in c#)

Use the Post-Redirect-Get (also known as Redirect after post) pattern. It simply consists in redirecting after a successful post, so that the user refresh results in an idempotent GET to be done rather than a POST. It has the additional advantage that the browser "forgets" about the POST in its history, which means the back button won't submit either.

Put a field in page call it status for example, after submitting first time it's populated with SUCCESS. If it is the do not save again.
Not the ultimate solution for sure.

Related

MVC how to store user searches so user can go back

Is such a thing possible? I have a search page and the user clicks a link to edit a document and it takes them to another page.
The user then hits the browser's back button i want to take them back to the results that they left off on so they don't have to search again.
EDIT: The search works as the user enters in parameters and it goes out to the database and returns results in a type of search result viewmodel. That viewmodel is parsed out to a table format and shown on the screen. Each row has certain bits of information tied to it like the primary key and other things that the user can see.
Once the user hits edit it uses the primary key to go back to the database to get the remaining data for that row and shows them a form on screen. If the user hits the back button on the browser I want to take them back to the result set that they just viewed without having to redo the search.
The way we have it setup is when the user hits back it goes to the index method that just does a new search page.
You can do this using the HTML5 History API. For instance, when the search returns its result you can use:
history.replaceState(..);
to modify the current browser history entry. It isn't compatible with some older browsers though, so if you have to support those, this may not work properly - or you'll have to find a workaround.
There exists a couple of 3rd party angular modules out there that wraps the history API, but it should work just fine on it's own.
A way I've done this is by using a controller extension method to get the page's search state from session. I'm sure there is a better way to do this in Angular though.

Caching issue with Firefox

I am working on an ASP.NET/MVC4 app and I fetch data continuously and my problem is related to caching.
The problem is that when I click on a particular link in my application it works fine, but sometimes it automatically redirects to the INDEX page that is the default page.
I surfed around about this problem and found that it's a problem in Mozilla that it maintains caching of every link. But sometimes some weird things happen and it automatically redirects a particular link to the INDEX page (301 Permanently REMOVED) and also stores it in the cache such that now every time I click on that link it always redirects me to the INDEX page that's been cached.
So now I have to clear the cache in my browser every time I face this problem.
How can I make it not automatically redirect to the cached INDEX page?
You should really expand on what exactly is happening at that particular link you mention because well it should not 301 redirect unless your telling it to.
Also you say I fetch data continuously. What does this mean to us? Why is this important to know? Explain if this changes the link or the data? Are you 404ing the older data or something? That could possibly explain why you 301 back to your index.
Now with the limited information we have been given by you... if you want to prevent firefox from caching your urls/redirects simply make your url have a querystring that updates which each request. Like using a timestamp.
For example: http://example.com/return-data.asp?timestamp=1350668920
Then each time you continuously fetch data update the page's link accordingly
For example: http://example.com/return-data.asp?timestamp=1350669084

C#, post, redirect and Back Button

There's a weird behavior where I use a post and then make a redirect to another page.
Then, if the user press the Back Button of his browser, the browser make a GET instead of a POST, so I lose the "viewstate". Why is the browser not "reposting" (or asking to repost) the data?
As defined by Wikipedia, PRG is “Post/Redirect/Get (PRG) is a common design pattern for web developers to help avoid certain duplicate form submissions and allow user agents to behave more intuitively with bookmarks and the refresh button.”
If you are interested in viewing the viewstate data of the POST request, before GET happens, then try using this utility: http://blog.getglimpse.com/2011/11/01/glimpse-0-86-released/

IE shows a previously cached version of my page

my scenario is this; the user selects the list of reports they wish to print, once they select and click on the a button, i open up another page with the selected reports ready for printing. I am using a session variable to pass reports from one page to another.
first time you try it, it works fine, second time you try it, it opens the report window with the previous selected reports. I have to refresh the page to make sure it loads the latest selections.
is there a way to get the latest value from the session every time you use it? or is there a better way to solve this problem. open for suggestions...
Thanks
C# Asp.net, IE&7 /IE 8
After doing some more checking maybe if you check out COMET it might help.
The idea is that you can have code in your second page which will keep checking the server for updated values every few seconds and if it finds updated values it will refresh itself.
There are 2 very good links explaining the imlementation.
Scalable COMET Combined with ASP.NET
Scalable COMET Combined with ASP.NET - Part 2
The first link explains what COMET is and how it ties in with ASP.NET, the second link has an example using a chat room. However, I'm sure the code querying for updates will be pretty generic and can be applied to your scenario.
I have never implemented COMET yet so I'm not sure how complex it is or if it is easy to implement into your solution.
Maybe someone developing the SO application is able to resolve this issue for you. SO uses some real-time feature for the notifications on a page, i.e: You are in the middle of writing an answer and a message pops up in your client letting you know someone else has added an answer and to click "here" to refresh.
The proper fix is to set the caching directives on the HTTP response correctly, so that the cached response is not reused without validation from the server.
When you fail to specify the cache lifetime, the client has to "guess" how long the response is good for, and the browser's guess probably isn't what you want. See http://blogs.msdn.com/b/ie/archive/2010/07/14/caching-improvements-in-internet-explorer-9.aspx
It's better to use URL paramaters. So you have a view of value of the paramaters.

Post data to different ASP.NET WebForm

In my ASP.NET WebForms application, I have a WebForm that contains an UpdatePanel and multiple views used for a wizard like interface.
At the end of the wizard, the user has an option of moving to another page by clicking a button. This new web page needs about 5 values from controls in the previous page.
What is the simplest way to do this? (Edit: ONLY using an HTTP POST with data - this is a requirement as I would use database/session otherwise)
I tried using cross-page posting with no luck, possibly because of my update panel and multiple views?
I tried using Server.Transfer, but this also breaks because of the update panel.
Important:
Data has to be sent via HTTP POST - The data can't be stored anywhere
The scenario can't be changed. I can't put everything on the same page
The simplest way to do this is by putting those values in the session object.
You could make a class that describes the data that you need to display on the redirected page. Instatiate a new instance of that at the time the user is filling out the wizard data, populate the new classes' object with the information you need, then add it to the session in the button_Click event before page redirection. On the page you are redirected to, grab the Session object, put it into a variable and extract the data you need.
I recommend you combine all the relevant pages into one; hiding panels that are not in play. ASP.NET will maintain the values of all the controls for you from post to post. The Viewstate was designed for sceneries like you describe. To keep to Viewstate size to a minimum, make sure you fill lookup values for drop-down controls in their "Init" methods.
You don't want to use the session state. The last thing you want is for the users to loose their data from previous pages because they took too long to answer.
If they're moving to another page in the solution, you have a few options.
ViewState - The ViewState is sent with the page delivery. It resides in the HTML, but is encrypted so no one can see the information. Depending on the size of the information, your page size could get rather large.
Session - This puts the information client-side via cookies.
Query String - Using the URI. This should only be used if it's non-sensitive information and if you don't want a user to be able to link back to the same action again.

Categories

Resources