Problem refresh in asp.net
I create Button. Add 开发者_如何转开发event Click. in event function AddToDataBase.
I press Button, event work, run function - data good add to database.
more I press F5 event wirk and function AddToDataBase start working.
It is not correct. how to fix it?
Try doing a redirect after you've done the "data good add to database", otherwise F5 will submit your event again.
By default, when a page is refreshed, the PostBack event is going to be registered again, which will fire off your button click event again.
A simple solution to this would be to add the following command after your AddToDatabase function completes:
Response.Redirect(Request.Url.PathAndQuery)
This will cause the page to redirect to itself, so that if a refresh occurs, the PostBack event will not register the button click.
It is not the most elegant, but it will get the job done. If you have more complex things going on with the page, you may need to look into other solutions, such as wrapping the AddToDatabase function through AJAX or something else.
F5 queries the server with same GET and POST parameters that were used last time to display the page. So if your button doesn't redirect you to another page, doing F5 will send a request to the server as if you've clicked that button again.
精彩评论