简体   繁体   中英

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?

完成“将数据良好添加到数据库”之后,请尝试进行重定向,否则F5将再次提交您的事件。

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM