2013-12-11 46 views
0

如何從按鈕切換gridview複選框?我只想使用Javascript。如何檢查/取消選中外部按鈕的Gridview複選框

我使用下面的代碼爲網格視圖:

<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; 
     } 
    } 
} 
+0

aspx的輸出結果如何顯示?你在Gridview中有頭文件嗎?問題可能是你的循環索引 – Ani

回答

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"; 
        } 
       } 
      } 
     } 
    } 
相關問題