为什么checkbox_CheckedChanged事件没有被触发?

时间:2013-05-01 03:52:35

标签: c# asp.net checkbox

我正在使用aspx文件中的gridview进行此操作

<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>

并且我想在单击复选框标题时选中所有复选框,因此我将一个事件添加到我的网格中,并将此代码添加到checkbox 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();
            //}
        }
好吧它现在正在工作,但现在的问题是,我的复选框事件只有在值转为检查/ true时才会被触发,但是当我取消选中它/将其变为false时,它不会触发,我应该修复哪个部分?

3 个答案:

答案 0 :(得分:1)

为什么要在服务器端做这件事。在java脚本中执行。这对性能水平会更好。使用此代码:::`function 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>`

答案 1 :(得分:0)

你的Gridview在复选框事件之前再次绑定,这就是为什么你的事件没有被调用而不是回发,所以把你的gridview绑定语句放在下面的块中。

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

答案 2 :(得分: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>