未选中时,子网格复选框选中的事件未被触发

时间:2015-06-02 12:15:04

标签: c# asp.net

我在父网格和子网格中设置了嵌套网格和复选框。父网格标题上有一个主要复选框,用于选择所有数据。然后,针对每个子行,针对父行和第三个复选框的另一个复选框。 前两个复选框和事件工作正常。 问题是子网格复选框。当我选中复选框时,事件会按预期触发。但是当取消选中该复选框时,事件不会被触发。相反,它只是回发。 代码如下。

 <asp:GridView ID="grdmaster" runat="server" SkinID="GridView" AutoGenerateColumns="false" DataKeyNames="ProjectNo" HorizontalAlign="Center" HeaderStyle-Height="35px" OnRowDataBound ="grdmaster_RowDataBound" >
                <Columns>

                    <asp:TemplateField HeaderText="View Forging">
                        <ItemTemplate>
                            <img alt="" style="cursor: pointer" src="images/plus1.png" />
                            <asp:Panel ID="pnlforginfo" runat="server" Style="display: none">
                                <asp:GridView ID ="gvforgmst" runat="server" AutoGenerateColumns="false"  CssClass="ChildGrid" ShowHeaderWhenEmpty="true" EmptyDataText="No Data Found" SkinID="GridView">
                                    <Columns>                                  
                                        <asp:BoundField ItemStyle-Width="150px" DataField="ForgingNo" HeaderText="Forging No" />
                                        <asp:BoundField ItemStyle-Width="150px" DataField="ProductType" HeaderText="Product" /> 
                                        <asp:TemplateField ControlStyle-Height="35px">

                                            <ItemTemplate>
                                                <asp:CheckBox ID="chkviewforg" runat="server" AutoPostBack ="true" OnCheckedChanged="chkviewforg_CheckedChanged" ViewStateMode="Enabled" EnableViewState="True" />
                                            </ItemTemplate>
                                        </asp:TemplateField>                                                                          
                                    </Columns>
                                </asp:GridView>
                            </asp:Panel>
                        </ItemTemplate>
                    </asp:TemplateField>                         
                    <asp:BoundField HeaderText="ProjectNo" DataField="ProjectNo" />
                    <asp:BoundField HeaderText="CustomerName" DataField="CustomerName" />                       
                    <asp:BoundField HeaderText="GradCode" DataField="GradCode" />
                    <asp:TemplateField ControlStyle-Height="35px">
                        <HeaderTemplate>
                            <asp:CheckBox ID="chkAll" runat="server"
                                AutoPostBack="true" OnCheckedChanged="chkAll_CheckedChanged" />
                        </HeaderTemplate>
                        <ItemTemplate>
                             <asp:CheckBox ID="chk" runat="server"
                                AutoPostBack="true" OnCheckedChanged="chk_CheckedChanged" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>






protected void chkviewforg_CheckedChanged(object sender, EventArgs e)
    {

        int cnt = 0;
        CheckBox chkAll = (CheckBox)grdmaster.HeaderRow.Cells[4].FindControl("chkAll");
        for (int i = 0; i < grdmaster.Rows.Count; i++)
        {
            GridView gvforgmst = (GridView)grdmaster.Rows[i].FindControl("gvforgmst");

            CheckBox chk = (CheckBox)grdmaster.Rows[i]
                         .Cells[4].FindControl("chk");
            foreach (GridViewRow gr in gvforgmst.Rows)
            {
                CheckBox chkviewforg = ((CheckBox)gr.FindControl("chkviewforg"));

                if (chkviewforg.Checked)
                {
                    cnt++;
                }
            }

            if (gvforgmst.Rows.Count > 0)
            {
                if (cnt == gvforgmst.Rows.Count)
                {
                    chk.Checked = true;
                }
                else
                {
                    chk.Checked = false;
                    chkAll.Checked = false;
                }
            }                
        }
    }

0 个答案:

没有答案