Firefox 上的 ASP.NET 按钮按两次的奇怪行为
如果我有这个asp.net按钮:
<asp:Button ID="Button_Save" Width="150px" OnClick="Button_Save_Click" runat="server" Text="Save" />
为了防止用户按两次按钮插入两条数据记录,我在后面的代码中添加了这个:
Button_Save.Attributes.Add("onclick", "this.disabled='true';" + ClientScript.GetPostBackEventReference(Button_Save_Data, null) + ";");
它在IE和Chrome上运行良好。然而,在 Firefox 中,每次用户按下按钮一次,我都会插入两条数据记录。
经过一段时间的谷歌搜索后,我通过添加以下内容修改了按钮: UseSubmitBehavior="false"
:
<asp:Button ID="Button_Save" Width="150px" UseSubmitBehavior="false" OnClick="Button_Save_Click" runat="server" Text="Save" />
然后它也适用于 Firefox,只插入一条数据记录。但随后我必须为网络应用程序上的每个按钮添加该设置。这需要做很多工作。
然而,我认为如果在 Firefox 上运行的 asp.net 应用程序总是出现这种情况,这确实是一个大问题。或者我实施了什么错误的事情?
提前致谢。
If I have this asp.net button:
<asp:Button ID="Button_Save" Width="150px" OnClick="Button_Save_Click" runat="server" Text="Save" />
In order to prevent the user to press the button twice to insert two data records, I added this in the code behind:
Button_Save.Attributes.Add("onclick", "this.disabled='true';" + ClientScript.GetPostBackEventReference(Button_Save_Data, null) + ";");
It works fine with IE and Chrome. However, in Firefox, every time the user presses the button once, I got two data records inserted.
After some time googling, I modified the button this way by adding: UseSubmitBehavior="false"
:
<asp:Button ID="Button_Save" Width="150px" UseSubmitBehavior="false" OnClick="Button_Save_Click" runat="server" Text="Save" />
Then it also works with Firefox, only one data record inserted. But then I will have to add that setting for every button on my web application. It requires a lot of work.
However, I think this is really a big problem if that is the case always with asp.net app running on Firefox. Or did I implement something wrong?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您最好在 SAVE 代码中添加检查以防止重复的 POST。我使用过像您这样的代码,但仍然有重复的记录,因为有人在错误的时间点击了“刷新”按钮。
编辑:
这是我使用的代码。关键是将服务器上的值与回发中返回的值相匹配。
在Page_Load中:
在Page_Prerender中
以及在Save例程中
You would be better off adding checks to your SAVE code to prevent duplicate POSTs. I've used code like yours and still had duplicate records because someone hit the Refresh button at the wrong time.
EDIT:
Here is the code I use. Key is to match a value on the server with a value coming back in the postback.
In Page_Load:
In Page_Prerender
And in the Save routine
您可以扩展
asp:button
并添加新功能,然后微调/替换所有asp:button
。接下来,您可以一次性更改所有按钮。You can extend
asp:button
and add your new functionality, then fine/replace all of yourasp:buttons
. Going forward you can then change all your buttons in once place.