0

I've got a asp.net gridview and inside of the grid view I have a check box at the header of the grid view like so:

<HeaderTemplate>
<asp:CheckBox Width="1px" ID="HeaderLevelCheckBox" AutoPostBack="true" OnCheckedChanged="SelectAllRows" runat="server" />
</HeaderTemplate>

This gives me a nice little check box at the top of the grid view...the event OnCheckedChanged calls a function called SelectAllRows that looks like this:

 Public Sub SelectAllRows(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim gr As GridViewRow = DirectCast(DirectCast(DirectCast(sender, CheckBox).Parent, DataControlFieldCell).Parent, GridViewRow)
        Dim h As CheckBox = DirectCast(gr.FindControl("HeaderLevelCheckBox"), CheckBox)

        For Each Row As GridViewRow In Me.gvLineItems.Rows
            Dim cb As CheckBox = CType(Row.FindControl("chkSelector"), CheckBox)
            cb.Checked = h.Checked
        Next
    End Sub

So if I click this header checkbox it checks all of the items in the gridview, and if I uncheck it, it unchecks all the items in the gridview. This works fine...but what doesnt seem to work is if the page loads up and I check the grid view header checkbox to true and it selects all the items in the gridview, then i click a button such as a DELETE button that calls some server side code. That code simply loops through the grid view and checks if the checkbox has been checked, if it is it calls code to delete an item. Something to this effect:

 For Each Row As GridViewRow In Me.gvLineItems.Rows
            Dim cb As CheckBox = CType(Row.FindControl("chkSelector"), CheckBox)
            Dim lID As Long = Convert.ToInt32(gvLineItems.DataKeys(Row.RowIndex).Value)
            If cb IsNot Nothing AndAlso cb.Checked Then
            'ok to delete
            End If
        Next

When I place a watch and debug on this it seems that the value cb is always false... Even though it was set to true when I clicked the header checkbox... What gives ???

The actual chkSelector in the grid view is for each row and it looks like this:

<ItemTemplate>
<asp:CheckBox ID="chkSelector" runat="server" onclick="ChangeRowColor(this)" />
</ItemTemplate>

Also I am already checking for postback..that is not the issue, remember chkSelector does not autopostback...

Thanks

oJM86o
  • 2,108
  • 8
  • 43
  • 71
  • Here Same problem posted some times ago http://stackoverflow.com/questions/1575445/checkbox-in-templatefield-in-gridview-loses-checked-on-postback/1576143#1576143 – Muhammad Akhtar May 06 '11 at 19:14

1 Answers1

1

I doubt, your gridview is rebinding on a Delete button click, because a click of the Delete button loads the page first where it will rebind and your checkbox's become unchecked again. I think you are binding your gridview some where in the page load event.

You have to do something like this

If(!Page.IsPostBack)
{
//Gridview Binding Code goes here....
}

Edit: Alternatively you can check/uncheck rows using javascript. It will save a round trip to the server side and resolve your current issue as well.

Here is complete code

<script language="javascript" type="text/javascript">
function SelectAll(spanChk,grdClientID) {
       var IsChecked = spanChk.checked;
       var Chk = spanChk;
          Parent = document.getElementById(grdClientID);           
          var items = Parent.getElementsByTagName('input');                          
          for(i=0;i<items.length;i++)
          {                
              if(items[i].type=="checkbox")
              {            
                 items[i].checked=document.getElementById(spanChk).checked;     
              }
          }     
    }

<HeaderTemplate>
 <asp:CheckBox runat="server" ID="chkHeader" onclick="SelectAll('<%=chkHeader.ClientID %>, <%=yourGrid.ClientID %>') />
</HeaderTemplate>

Ajean
  • 5,528
  • 14
  • 46
  • 69
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
  • are you checked your checkbox in javascript ? if so show your code of javascript, then there must be the problem in your javascript code. – Muhammad Akhtar May 06 '11 at 18:48
  • @Muhamma Akhtar - Are you reading my post correctly? Please look as I stated on the gridview there exists a header in which there is a checkbox in the header. The event for that is OnCheckedChanged (again please read my post). The code is posted for OnCheckedChanged, the check boxes are set to true / false for the checked status here... – oJM86o May 06 '11 at 18:51
  • Problem here is, your Gridview is rebinded; Upon clicking of header checkbox, you checked all the checkbox on server side. Can I provide you the code of javascript? It do the same as what you are doing on server side. It will save the round trip to server and I hope your current problem will be solved as well. – Muhammad Akhtar May 06 '11 at 18:56
  • @Muhammad Akhtar - I'm not sure what you want me to do... do you mean you can provide a better solution? If so just post it as a new answer and fully explain how to call it.... this is asp.net and vb.net with a gridview within an updatepanel. – oJM86o May 06 '11 at 18:58
  • @Muhammad Akhtar - I dont think your solution will solve my issue. At the end of the day I will still be clicking an image button which calls server side code to `delete` the checked items in my gridview. That is my issue, its as if the server side code does not see the items as checked... – oJM86o May 06 '11 at 19:01
  • Modified answer, please check this and let me know your findings. You only need to copy the JS code and Change the ID of your GridView in when call JS SelectAll fucntion – Muhammad Akhtar May 06 '11 at 19:09
  • Here is same problem posted by someone some times ago http://stackoverflow.com/questions/1575445/checkbox-in-templatefield-in-gridview-loses-checked-on-postback/1576143#1576143 – Muhammad Akhtar May 06 '11 at 19:11
  • @Muhammad Akhtar - AHA I got what you mean now, this is exactly what I was looking for +100 :) – oJM86o May 06 '11 at 19:22
  • Always try to understand the solution first and then try to down vote. As here its late night and I was preparing myself to sleep. But I was shocked to your comments that I have not understand your question. That's I have wait your comments. As I was sure that my solution will work and I understand your question very well. – Muhammad Akhtar May 06 '11 at 19:27