Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checked checkboxes not showing checked in gridview

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
US
I have 3 reports where I have a checkbox column as the first column which a checkbox in the header as my select/unselect all checkbox. I use jquery to select/unselect all. Two of the reports do this select/unselect all correctly all of the time. One of the reports only do it correctly once.

The jquery code and checkboxes in the gridview are the same. Which makes sense since the behavior is correct the first time.

What is happening is that the checkboxes are getting set correctly but the browsers (ie, chrome and firefox) all do not show the checkboxes as selected in the rows (but the one in the header does). I can see this in the Developer tools. All the checkboxes show as checked when the header checkbox is selected and as unselected when the header checkbox is unselected.

I can manually, check the boxes in some of the rows and when unselected all, they disappear but when I then press the select all checkbox they still show on the screen as unselected (but as selected in the developer tools).

Any idea why that is the case?

It almost seems like the grid is getting rebound but that isn't the case.

Thanks,

Tom
 
It's hard to say. I would first check all of your JQuery code to make sure there isn't some code doing something you don't want, like changing a visible or display property.

Post your jquery code and gridview markup
 
Unfortunately, I can't. It is really a large sheet.

The jquery code is:

Code:
         $(".selectall").change(function () {
            var checkBoxSelectorAll = '#<%=gvSelectionGrid.ClientID%> input[id*="chkSelectAll"]:checkbox';
            var checkBoxSelectorRows = '#<%=gvSelectionGrid.ClientID%> input[id*="chkSelected"]:checkbox';

            $(checkBoxSelectorRows).attr('checked', $(checkBoxSelectorAll).is(":checked"));
         });

The code in the GridView is:

Code:
<asp:TemplateField HeaderText="Select" HeaderStyle-ForeColor="white" HeaderStyle-Width="25px">
   <HeaderTemplate>
      <asp:CheckBox ID="chkSelectAll" runat="server" CssClass="selectall"  />
   </HeaderTemplate>
   <ItemTemplate>
      <asp:CheckBox ID="chkSelected" runat="server" style="margin-right: 2px" CssClass="selectrow" />
      <asp:HiddenField ID="hdnCAParticipantID" runat="server" Value='<%# Bind("CAParticipantID") %>' />
   </ItemTemplate>
   <HeaderStyle ForeColor="White" Width="25px" />
</asp:TemplateField>

I found out how to make it work.

Everything above is the same but I add an AutoPostback = "true" in the first checkbox (the one in the Header) and it works fine. The code behind just goes to Page_Load but doesn't do anything (no rebinding). But it works.

It's like the checkboxes will not redisplay the "check" in the checkbox if it was already done once (even though it does add the checked="checked to the checkbox each time the select all checkbox is checked).

Same with all browsers. It's like you need to do a postback to reset the objects first.
 
You should only need to bind once by putting your bind code in and If Not page.IsPostBack End If

If you are adding controls dynamically then you will need to add them each time. It's really hard to know and see what the issue is without seeing the code.
I know it is working, but I am assuming it is a side affect of posting the page again when you really don't need to.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top