如何选中/取消选中外部按钮的Gridview复选框

时间:2013-12-11 14:33:34

标签: javascript asp.net gridview

如何从按钮切换gridview复选框?我只想使用Javascript。

我在网格视图下面使用以下代码:

<asp:GridView ID="gvWrkLogVW" runat="server" AutoGenerateColumns="False" GridLines="None" BorderStyle="None" ShowHeader="False">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="chkWorklog" runat="server"></asp:CheckBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="The_Text" />
    </Columns>
</asp:GridView>

我想从外部按钮

访问代码gridview复选框
<asp:Button ID="btncheck" runat="server" Text="Check All" OnClientClick="SelectAll()" />

我正在编写以下javascript,但它无效

 function SelectAll() {
     var gridViewControl = document.getElementById('<%= gvGetAllCircuits.ClientID %>');
         for (i = 0; i < gridViewControl.rows.length; i++) {
             if (gridViewControl.elements[1].type == "checkbox") {
                 gridViewControl.elements[1].checked = true;
         }
     }
 }

1 个答案:

答案 0 :(得分:0)

<强> JS

function SelectAll() {
        var gridViewControl = document.getElementById('<%= gvGetAllCircuits.ClientID %>');
        for (i = 0; i < gridViewControl.rows.length; i++) {

          //loop starts from 1. rows[0] points to the header.
            for (i=1; i<gridViewControl.rows.length; i++)
            {
                //get the reference of first column - if you have a header
                cell = gridViewControl.rows[i].cells[0];

                //loop according to the number of childNodes in the cell
                for (j=0; j<cell.childNodes.length; j++)
                {           
                    //if childNode type is CheckBox                 
                    if (cell.childNodes[j].type =="checkbox")
                    {
                    //assign the status of the Select All checkbox to the cell checkbox within the grid
                        cell.childNodes[j].checked = "true";
                    }
                }
            }
       }
   }