开发者

grid view checkbox and javascript not informing server side code?

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


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>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜