简体   繁体   English

asp.net中的问题刷新

[英]Problem refresh in asp.net

I create Button. 我创建按钮。 Add event Click. 添加事件单击。 in event function AddToDataBase. 在事件函数AddToDataBase中。

I press Button, event work, run function - data good add to database. 我按下按钮,事件工作,运行功能-数据很好地添加到数据库中。

more I press F5 event wirk and function AddToDataBase start working. 更多信息我按F5事件,功能AddToDataBase开始工作。

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. F5使用与上次显示页面相同的GET和POST参数查询服务器。 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. 因此,如果您的按钮没有将您重定向到另一个页面,则执行F5会将请求发送到服务器,就像您再次单击该按钮一样。

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. 默认情况下,刷新页面时,将再次注册PostBack事件,这将再次触发您的按钮单击事件。

A simple solution to this would be to add the following command after your AddToDatabase function completes: 一个简单的解决方案是在AddToDatabase函数完成后添加以下命令:

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. 这将导致页面重定向到其自身,因此,如果发生刷新,则PostBack事件将不会注册按钮单击。

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. 如果页面上发生了更复杂的事情,则可能需要研究其他解决方案,例如通过AJAX或其他方式包装AddToDatabase函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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