ASP.Net - Help with datagrid/checkboxes/double submit

Posted by Gareth D on Stack Overflow See other posts from Stack Overflow or by Gareth D
Published on 2009-02-10T10:13:12Z Indexed on 2010/05/27 22:01 UTC
Read the original article Hit count: 193

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?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about datagrid