2009-11-12 76 views
0

我似乎無法得到checkAll和uncheckAll的LinkBut​​ton功能使用JavaScript時,我的GridView裏面的UpdatePanel選擇所有全部不LinkBut​​ton的不工作時的GridView裏面的UpdatePanel

<asp:LinkButton ID="LinkButton1" runat="server" Text="Check All" OnClientClick="CheckOrUnCheckAll(true);return false;"></asp:LinkButton> 
        <asp:LinkButton ID="LinkButton2" runat="server" Text="Uncheck All" OnClientClick="CheckOrUnCheckAll(false);return false;"></asp:LinkButton> 
        <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
         <ContentTemplate> 
          <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" 
           ShowHeaderWhenEmpty="True" EmptyDataText="No Uploads are found" ShowEmptyTable="True" 
           ShowFooterWhenEmpty="False" Style="margin-top: 20px" OnSelectedIndexChanged="GridView2_SelectedIndexChanged" 
           OnRowDataBound="GridView2_RowDataBound" OnRowCommand="GridView2_RowCommand" DataKeyNames="Id" 
           PageSize="8" AllowPaging="True" AlternatingRowStyle-BackColor="#C2D69B"> 
           <AlternatingRowStyle BackColor="#C2D69B"></AlternatingRowStyle> 
           <Columns> 
            <asp:TemplateField HeaderText="Select"> 
             <HeaderTemplate> 
              <asp:CheckBox ID="checkAll" runat="server" onclick="checkAll(this)" AutoPostBack="True" /> 
             </HeaderTemplate> 
             <ItemTemplate> 

              <asp:CheckBox ID="CheckBox1" runat="server" onclick="Check_Click(this)" AutoPostBack="True" /> 
             </ItemTemplate> 
            </asp:TemplateField> 

回答

2

嘗試javascript函數以下工作:

function SelectAll(chk) 
     { 
      //get reference of GridView control 
      var grid = document.getElementById("<%= GridView1.ClientID %>"); 
      //variable to contain the cell of the grid 
      var cell; 

      if (grid.rows.length > 0) 
      { 
       //loop starts from 1. rows[0] points to the header. 
       for (i=1; i<grid.rows.length; i++) 
       { 
        //get the reference of first column 
        cell = grid.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" && cell.childNodes[j].id.indexOf('chkSel')!=-1) 
         { 
         //assign the status of the Select All checkbox to the cell checkbox within the grid 
          cell.childNodes[j].checked = chk; 
         } 
        } 
       } 
      } 
     } 

嘗試以下ASPX代碼:

<asp:LinkButton ID="lnkCheck" Text="Check all" OnClientClick=" SelectAll(true); return false;" 
       runat="server"></asp:LinkButton><br /> 
      <asp:LinkButton ID="lnkUncheck" Text="Uncheck all" OnClientClick=" SelectAll(false); return false;" 
       runat="server"></asp:LinkButton><br /> 
      <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
       <ContentTemplate> 
        <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server"> 
         <Columns> 
          <asp:TemplateField HeaderText="CategoryID"> 
           <HeaderTemplate> 
            Select 
            <asp:CheckBox ID="chkAll" runat="server" onclick="SelectAll(this.checked);" /> 
           </HeaderTemplate> 
           <ItemTemplate> 
            <asp:CheckBox ID="chkSel" runat="server" /> 
           </ItemTemplate> 
          </asp:TemplateField> 
          <asp:TemplateField HeaderText="CategoryID"> 
           <ItemTemplate> 
            <asp:CheckBox ID="chkOther" runat="server" /> 
           </ItemTemplate> 
          </asp:TemplateField> 

          <asp:TemplateField HeaderText="Category"> 
           <ItemTemplate> 
            <asp:LinkButton ID="lnkID" runat="server" CommandName="sel" CommandArgument='<%# DataBinder.Eval(Container,"DataItem.CategoryID") %>' 
             Text='<%# DataBinder.Eval(Container,"DataItem.CategoryName") %>'></asp:LinkButton> 
           </ItemTemplate> 

          </asp:TemplateField> 

         </Columns> 
        </asp:GridView> 

       </ContentTemplate> 
      </asp:UpdatePanel> 
+0

感謝名單一噸......這對我的作品 – Mishigen 2009-11-12 12:22:20

相關問題