0

我正在使用我在 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>

我想在单击复选框标题时检查所有复选框,因此我将一个事件添加到我的网格中,并将此代码添加到复选框 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时,它没有触发,我应该修复什么部分?

4

3 回答 3

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>`
于 2013-05-01T04:51:57.220 回答
0

您的 Gridview 在复选框事件之前再次绑定,这就是为什么不调用您的事件而不是回发的原因,因此将您的 gridview 绑定语句放在以下块中。

  if (!Page.IsPostBack)
        {
 gridDepartement.DataSource = adp.GetDataDepartement();
                gridDepartement.DataBind();
}
于 2013-05-01T04:50:26.793 回答
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>
于 2013-05-01T07:57:18.070 回答