ASP.NET: web form button onclick issue - c#

Im implementing Web Form using ASP.NET in c#, I have a page with few TextBox and a Submit Button, the data from TextBox going to insert into Database when Submit Button is pressed, the page will be retain and no redirect is required.
However, there is a issue after the data has been submitted, if F5/refresh the page without click on the Submit Button, the data will insert again and again.
How can I avoid such issue happen after the first submission?
Thank you in advanced.

Place Response.Redirect to the same page after save logic done. This happens because browser repeat last request on F5 in you case this is POST request to save. After Response.Redirect last request will be GET and you problem will be solved

Unfortunately you can't prevent something like that because the browser excute the same code again after you press refresh so thier is nothing you can do there but you can use response.redirect to the same page again.
Also you can check for duplication in the database so if that record exist don't save it again.
Hope I Helped

Related

Clicking Back button in browser Expires the page

I'm using .NET c#.
I have a page for search which displays results on next page.
If you select a Course and that's not the one you want,you can click back IN BROWSER and go to search results to select another.
But clicking back in browser causes expired page.
I have a back button which works fine but clicking back in browser expirse the data.
How do i fix this?
The data on the expired page are sent by POST method. It will require user to re-send the data. What you can do is change form method to GET (not always acceptable due to data size, security, url readability, logging and other factors). If GET is not an option, you should cache the POST result somehow (for example, putting it in a session) and make a redirect to a clean page which will take and display the cached result without requiring user to re-send it. When you return via Back button to that page, there will be no problem because that page is not generated directly by POST request, it only shows the results.
If the POST result is too big to be cached, then simply cache the input parameters and build a query based on them in the "clean" page. Once it has displayed the result, the Back button will work without the need to reload.

How to stop form resubmission on browser back button being pressed by the client

How can I stop my form from being resubmitted in asp.net when the client clicks on the browser back button.
You can prevent the resubmission of a page redirecting the POST action to a GET one.
Keep a look in PRG Pattern and how implement it in C# if using MVC or try try this approach

Clear details stored in form

I have a user registration page, after entering all the details and successful account creation, I want to stay in the same page, but all the user entered details should be cleared
but if the registration is unsuccessful then all the user entered details should be retained.
which is the right way of achieving this?
is clearing all the values manually in code, the only way?
Viewstate is maintained during postbacks. So, you can do a redirect to the same url if the registration is successful. Make sure that your databindings check for IsPostback.
The state for the textboxes is maintained during postback, so you could clear them manually.
However Ingenu's answer suggest to use whats called the PRG-pattern which is an even better fit. In your case it goes like this:
On succesfull creation of an account, you should issue a redirect to the same page. The redirect will call your registration page as a GET request again, and clear all textboxes.
If the create is unsuccessfull, you should NOT redirect. So that the user stays on the same page with the textboxes still filled.

How can I prevent some of my views in ASP.NET MVC from being cached?

I have a view that shows a list of items and when you click on one of them it will take you to the items page. I also store a "Viewed" flag in the database for that item.
Now, when the user clicks back, the item should be styled differently because of the change in the "Viewed" flag. However, every time I click back, it is as it was before I clicked into the item, and I have to click refresh to see the actual state of the page now.
How can I prevent this page from being cached so when a user clicks back they will see the latest version of this site, complete with the new styling?
Mark the controller action that generates the list with the OutputCacheAttribute and set the cache location to none to prevent that page from being cached on the client. This should cause the client to request the page again. If the user is using the back button, however, I think that the page is served up by the browser without reloading regardless of the caching. At least in FF I don't see it requesting the page again using Firebug.
[OutputCache( Location = OutputCacheLocation.None )]
Call this in your controller action:
Response.Cache.SetCacheability(HttpCacheability.NoCache)
This will prevent the browser from caching the page.

New row inserted for each page refresh

Hi I'm getting a strange problem while inserting records into database.
In my button click event I'm trying to insert some values into my database it is working fine. Once insertion is completed... again if I press F5 or refresh the browser a new row is getting inserting with the previous values in the table.
Why it is happening?
Thank you
When you click the button, it sends a POST request to the server, and the updated page is sent back as the response. In order to refresh that page, the same POST must be made again, and the server interprets this as another button click.
Most Web browsers give a warning in this situation, saying that refreshing the page may repeat any action that was just performed (in your case, inserting a row in the database). But if you want to prevent this from happening at all, the best way is probably to respond to the POST request with a redirect. In ASP.NET with C#, the way to do this is:
Response.Redirect(url);
Redirecting back to the same page is fine, or you could also redirect to a different page. When the browser receives this redirect, it will issue a GET request for the specified page. Then if you refresh, no action will be taken.
you need to handle you page refresh event handler and check.
Simply because refresh caused another postback which re-do the insertion operation.
You might want to perform a redirect to the same page to prevent that. There are some other means to handle this issue as well.
It's happening because you're requesting the page again, using the same POST parameters that were used to request it the first time.
To fix it: either check for a page refresh in your code and don't do the insert in that event, or set a cookie the first time, and then don't do the insert again if the cookie is there. The cookie could either have a fixed duration (say 30 minutes), or it could be a session cookie, which would last until the users closes the browser.
Check the solution in the following link
http://aspalliance.com/687_Preventing_Duplicate_Record_Insertion_on_Page_Refresh.4

Categories

Resources