Upon clicking a gridview row, I am able to check a column of checkboxes. However, this functionality is limited to a specific column only, as clicking directly on the checkbox itself does not trigger the action.
For example, when clicking anywhere in this section, the checkbox works as expected and remains checked:
https://i.sstatic.net/GXMSz.jpg
But, if I click on this other part, the checkbox functionality ceases to work:
https://i.sstatic.net/tBFNJ.jpg
Below are the relevant code snippets:
$(document).ready(function () {
$('body').on('click', 'tr.dataRow', function () {
var checked = $(this).find('input[id*=chkBusinessSelected]').prop('checked');
$(this).find('input[id*=chkBusinessSelected]').prop('checked', !checked);
});
});
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(function () {
$('body').on('click', 'tr.dataRow', function () {
var checked = $(this).find('input[id*=chkBusinessSelected]').prop('checked');
$(this).find('input[id*=chkBusinessSelected]').prop('checked', !checked);
});
});
<asp:GridView ID="grdBusiness" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical" Width="420px" ShowHeaderWhenEmpty="True" EmptyDataText="No records Found" OnRowDataBound="grdBusiness_RowDataBound" DataSourceID="SqlDataSource3" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkBusinessSelected" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Code" HeaderText="Code" SortExpression="Code" />
<asp:BoundField DataField="Business Type" HeaderText="Business Type" SortExpression="Business Type" />
</Columns>
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#90FF90" Font-Bold="True" ForeColor="Black" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
protected void grdBusiness_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.CssClass = "dataRow";
}
}