-1

I am able to check a column of checkbox when I click the gridview row, however it limits only to a particluar column, when I click on the column of the checkbox, it it is not wkorking.

Here's the case, When I click the any where on this part, it worked well. the checkbox still checking.

enter image description here

but when I click on this part, it doesn't work already.

enter image description here

codes are here

$(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";     
     }
 }
2
  • What are you trying to achieve? Commented Aug 12, 2015 at 10:26
  • to have a real FULL gridview row that is able to check the checkbox, as of now the codes are only able to do that on the 2nd and 3rd column. checkbox column when click is uable to check itself. Commented Aug 12, 2015 at 10:30

3 Answers 3

2

I am edited @UsmanKhalid 's answer and it worked for me:

$(function () {
        $("#<%=grdBusiness.ClientID %> td").click(function () {
            selectRow($(this).closest("tr"));
        });
    });

    function selectRow(row) {
        var firstInput = row[0].getElementsByTagName('input')[0];
        firstInput.checked = !firstInput.checked;
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Should I put anything in the .ascx source page?
I have a page and gridview in page and this script at the header. At my page it works. By the way are you added Jquery reference to your page or control?
I see.. my javascript is on external file. I have a user control with Update Panel on it.
1

Try this code:

$(function () {
        $("[id*=GridView] td").click(function () {
            selectRow($(this).closest("tr"));
        });
    });

function selectRow(row)
{
    var firstInput = row.getElementsByTagName('input')[0];
    firstInput.checked = !firstInput.checked;
}

8 Comments

Just replace "[id*=GridView]" with your gridview id.
should I removed my original Javascript code, and replace yours? or retrain it and yours is just an addtion?
Yes remove your code. And put your gridid like this : #<%=grdBusiness.ClientID %> td
I'm sorry I need to remove my vote for this because i found out that this does not fully answers the question, because it still does not click the checkbox, only the column part but the checkbox itself is not checkable.
@rickyProgrammer change row.getElementsByTagName('input')[0]; to row[0].getElementsByTagName('input')[0]; and try again. It will works. Because row is jquery variable and getElementsByTagName is javascript function.
|
0

Try the following

$('tr.dataRow').click(function(){

  var chkBox = $(this).find('input[type=checkbox]');
    chkBox.prop("checked", !chkBox.prop("checked"));

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.