2013-05-01 83 views
0

我正在做這個跟我的GridView控件在aspx文件爲什麼checkbox_CheckedChanged事件不會被觸發?

<asp:GridView ID="gridDepartement" runat="server" CellPadding="4" ForeColor="Black" 
        GridLines="Horizontal" AutoGenerateColumns="False" BackColor="White" 
        BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" AllowSorting="True" > 
        <Columns> 
         <asp:templatefield> 
          <HeaderTemplate> 
           <asp:CheckBox ID="cbSelectAll" 
            runat="server" AutoPostBack="true" 
            OnCheckedChanged="cbSelectAll_CheckedChanged" /> 
          </HeaderTemplate> 
          <itemtemplate> 
           <asp:CheckBox Id="cbSelectOne" runat="server"/> 
          </itemtemplate> 
         </asp:templatefield> 
         <asp:CommandField ShowEditButton="True" ItemStyle-Width="20"/> 
         <asp:CommandField ShowDeleteButton="True" ItemStyle-Width="20"/> 
         <asp:CommandField ShowSelectButton="True" ItemStyle-Width="20"/> 
         <asp:boundfield headertext="Departement Code" datafield="departementcode" 
          ItemStyle-HorizontalAlign="Center"/> 
         <asp:boundfield headertext="Departement Name" datafield="departementname" 
          ItemStyle-HorizontalAlign="Center" /> 
         <asp:boundfield headertext="Created By" datafield="createby" 
          ItemStyle-HorizontalAlign="Center" /> 
         <asp:boundfield headertext="Created Date" datafield="createdate" 
          ItemStyle-HorizontalAlign="Center" /> 
         <asp:boundfield headertext="Updated By Name" datafield="updateby" 
          ItemStyle-HorizontalAlign="Center" /> 
         <asp:boundfield headertext="Last Update" datafield="lastupdate" 
          ItemStyle-HorizontalAlign="Center" /> 
        </Columns> 
        <FooterStyle BackColor="#CCCC99" ForeColor="Black" /> 
        <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" /> 
        <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" /> 
        <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" /> 
        <SortedAscendingCellStyle BackColor="#F7F7F7" /> 
        <SortedAscendingHeaderStyle BackColor="#4B4B4B" /> 
        <SortedDescendingCellStyle BackColor="#E5E5E5" /> 
        <SortedDescendingHeaderStyle BackColor="#242121" /> 
       </asp:GridView> 

,我想檢查所有複選框時複選框頭點擊,我將事件添加到我的網格,並在複選框checkchanged事件添加以下代碼:

protected void cbSelectAll_CheckedChanged(object sender, EventArgs e) 
     { 
      bool chkFlag = false; 

      CheckBox cbHD = (CheckBox)gridDepartement.HeaderRow.FindControl("cbSelectAll"); 

      if (cbHD.Checked) 
      { 
       chkFlag = true; 
      } 
      foreach (GridViewRow dr in gridDepartement.Rows) 
      { 
       CheckBox chk = (CheckBox)dr.Cells[0].FindControl("cbSelectOne"); 
       chk.Checked = chkFlag; 
      } 
     } 

頁面加載代碼:

protected void Page_Load(object sender, EventArgs e) 
     { 
      //if(!IsPostBack) 
      //{ 
       DataSourceDepartement dpt = new DataSourceDepartement(); 

       DataSourceDepartementTableAdapters.departementTableAdapter 
       adp = new DataSourceDepartementTableAdapters.departementTableAdapter(); 

       //bind gridview to datatable 
       gridDepartement.DataSource = adp.GetDataDepartement(); 
       gridDepartement.DataBind(); 
      //} 
     } 

確定它的工作了,但現在的問題是,我的複選框事件僅trigered當它爲v alue轉爲檢查/ true,但是當我取消選中/將其設爲false時,它不會觸發,我應該修復哪些部分?

+0

哪個事件綁定你的GridView? – 2013-05-01 04:44:54

+0

頁面加載事件。 – NomNomNom 2013-05-01 04:46:04

+0

你可以把代碼 – 2013-05-01 04:46:38

回答

0
function SelectAll(objcheckbox) 
     { 
      var HeaderCBControl = objcheckbox; 
      //var table = getParentByTagName(HeaderCBControl, 'table'); 
      var Inputs = document.getElementById('CenterContent_gridDepartement').getElementsByTagName("input"); 

      for (var n = 0; n < Inputs.length; ++n) 
       if (Inputs[n].type == 'checkbox') 
       { 
        Inputs[n].checked = HeaderCBControl.checked; 
       } 
      return false; 
     } 
     </script> 
0

你的Gridview在複選框事件之前再次被綁定,這就是爲什麼你的事件沒有被調用而不是回發所以把你的gridview綁定語句放在下面的塊中。

if (!Page.IsPostBack) 
     { 
gridDepartement.DataSource = adp.GetDataDepartement(); 
       gridDepartement.DataBind(); 
} 
+0

我試過了,但問題是,如果我把內部綁定代碼!page.ispostback,做了回發後,我的網格不顯示任何東西。 – NomNomNom 2013-05-01 05:30:35

+0

只是把整個頁面加載代碼在該塊 – 2013-05-01 08:54:48

1

爲什麼要在服務器端做這件事。在java腳本中執行。這將是更好的性能水平。使用此代碼:::`功能CheckAll(objparentcheckbox){ var HeaderCheckboxControl = objparentcheckbox var table = getParentByTagName(HeaderCheckboxControl,'table');

 //get all the control of the type INPUT in the base control. 
     var Inputs = table.getElementsByTagName("input"); 

     for (var n = 0; n < Inputs.length; ++n) 
      if (Inputs[n].type == 'checkbox') { 
       Inputs[n].checked = HeaderCheckboxControl.checked; 
      } 
     return false; 
    } and in grid:::: <HeaderTemplate> 
             <asp:CheckBox ID="ChkSelectAll" onclick="CheckAll(this)" runat="server" /> 
            </HeaderTemplate>` 
+0

王子它顯示此錯誤ReferenceError:getParentByTagName未定義。 – NomNomNom 2013-05-01 05:59:11

+0

我不知道爲什麼會出現這個錯誤。你可以通過使用Fire Bug控制檯來檢查這個錯誤。否則,我有更好的解決方案。請檢查這個鏈接::::: http://www.aspdotnet-suresh.com/2011/03/how-to-selectdeselect-checkboxes-in.html – Prince 2013-05-01 06:49:16

+0

現在它正在工作,謝謝你的王子。 – NomNomNom 2013-05-01 07:56:34

相關問題