有关 DataGrid/CheckBoxes/双重提交的帮助
我们有一个简单的数据网格。 每行都有一个复选框。 复选框设置为自动回发,并且隐藏代码具有用于复选框检查更改事件的事件处理程序。 这一切都按预期进行,没有什么复杂的。
然而,我们希望在选中一个复选框后立即禁用该复选框,以防止重复提交,即选中复选框,通过客户端 JavaScript 提交表单来禁用所有复选框。
为了实现这一点,我们将一些代码注入到 onclick 事件中,如下所示(请注意,警报仅用于测试!):
Protected Sub DgAccounts_ItemCreated(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DgAccounts.ItemCreated
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim chk As CheckBox = CType(e.Item.FindControl("chkItemChecked"), CheckBox)
chk.Attributes.Add("onclick", "alert('fired ...');DisableAllDataGridCheckBoxes();")
End If
End Sub
当检查渲染页面的源时,我们得到以下内容:
<input id="DgAccounts__ctl2_chkItemChecked" type="checkbox" name="DgAccounts:_ctl2:chkItemChecked" onclick="alert('fired ...');DisableAllDataGridCheckBoxes();setTimeout('__doPostBack(\'DgAccounts$_ctl2$chkItemChecked\',\'\')', 0)" language="javascript" />
一切都按顺序出现,但是服务器端事件不会触发 - 我相信这是由于复选框被禁用,就好像我们只是保留警报并删除调用以禁用复选框一样,一切正常。
即使禁用了该复选框,我也可以强制触发 check-change 事件吗?
We have a simple datagrid. Each row has a checkbox. The checkbox is set to autopostback, and the code-behind has an event handler for the checkbox check-changed event. This all works as expected, nothing complicated.
However, we want to disable the checkboxes as soon as one is checked to prevent a double submit i.e. check box checked, all checkboxes are disabled via client side javascript, form submitted.
To achieve this I we are injecting some code into the onclick event as follows (note that the alert is just for testing!):
Protected Sub DgAccounts_ItemCreated(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DgAccounts.ItemCreated
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim chk As CheckBox = CType(e.Item.FindControl("chkItemChecked"), CheckBox)
chk.Attributes.Add("onclick", "alert('fired ...');DisableAllDataGridCheckBoxes();")
End If
End Sub
When inspecting the source of the rendered page we get the following:
<input id="DgAccounts__ctl2_chkItemChecked" type="checkbox" name="DgAccounts:_ctl2:chkItemChecked" onclick="alert('fired ...');DisableAllDataGridCheckBoxes();setTimeout('__doPostBack(\'DgAccounts$_ctl2$chkItemChecked\',\'\')', 0)" language="javascript" />
It all appears in order, however the server side event does not fire – I believe this is due to the checkbox being disabled, as if we just leave the alert in and remove the call to disable the checkbox it all works fine.
Can I force the check-changed event to fire even though the check box is disabled?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我自己没有测试过,但是当您调用“DisableAllDataGridCheckBoxes”方法时,是否需要禁用已选中的复选框?
如果没有,您可以传递调用复选框的 ID,然后仅禁用所有其他复选框。
我认为这将允许服务器端代码触发。
I haven't tested this myself but when you call your 'DisableAllDataGridCheckBoxes' method, do you need to disable the checkbox that has been checked?
If not, you could pass the calling checkbox's id and then only disable all the other checkboxes.
I would think this would then allow the server side code to fire.
服务器端事件未触发的原因是禁用的复选框不会与表单的其余部分一起提交。
我使用的解决方法是将复选框的 onclick 事件设置为 null 而不是禁用它们 - 虽然这不会向用户提供后续点击被忽略的视觉线索,但它确实可以防止双重提交,并且复选框设置在顶部呈现响应时的正确状态。
The reason the server side event is not firing is that disabled checkboxes do not get submited with the rest of the form.
The workaround I have used is to set the onclick event of the checkboxes to null rather than disable them - while this gives no visual clue to the user that subsequent clicks are ignored, it does prevent the double submit, and the check boxes are set top the correct state when the response is rendered.
不要禁用复选框(然后不会提交),只需“阻止”它们被选中即可。 通过更改其 onclick 处理程序以重置选中状态来执行此操作。 您可以将所有其他复选框设置为禁用,并仅针对正在处理的复选框执行此操作。 就像是:
Instead of disabling the checkboxes (which then do not get submitted), just "prevent" them from being selected. Do this by changing their onclick handler to reset the checked state. You coul dset all of the other checkboxes to disabled, and do this just for the one that is processing. Something like:
一种可能的解决方法是在数据网格的 RowCommand 的服务器端代码中完成所有这些操作。
(在 C# 中发布示例,但我希望在 VB 中不会有太大不同)
则可以使用
或者如果您只想禁用特定项目,
。 但是,如果您只想在客户端代码中实现此功能,那么它对您没有帮助。
A possible workaround, would be to do all this in the server side code of the RowCommand of the data grid.
(posting the sample in C# but won't be much different in VB I hope)
or you could use
if you only want to disable the specific item.
However if you only want to implement this in client side code, it won't help you.