当另一个打开嵌套的GridView时如何关闭

时间:2019-01-29 15:33:12

标签: c# asp.net

我有一个带有嵌套gridview的网格视图。问题是,当我打开一个,然后打开另一个,前一个停留打开。我如何才能将其关闭?除非我必须在jquery上这样做,否则我无法弄清楚如何在codebehind上做到这一点

<script type="text/javascript">
    $(function () {
        $("[id*=ibtnShowLocalizacoes]").each(function () {
            if ($(this)[0].src.indexOf("minus") != -1) {
                $(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>");
                $(this).next().remove();
            }
        });
        $("[id*=ibtnShowEtiquetas]").each(function () {
            if ($(this)[0].src.indexOf("minus") != -1) {
                $(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>");
                $(this).next().remove();
            }
        });
    });
</script>

,这是嵌套的gridview。警告它很长,因为它是3级嵌套

<asp:GridView
    ID="GridView1"
    runat="server"
    AutoGenerateColumns="False"
    DataKeyNames="InventarioID"
    CssClass="mx-auto text-center"
    BackColor="White"
    BorderColor="#000000"
    BorderStyle="Solid"
    BorderWidth="1px"
    CellPadding="4"
    GridLines="Both"
    AllowPaging="True"
    Width="1000px"
    ShowHeaderWhenEmpty="true"
    EmptyDataText="Sem Registos"
    PagerSettings-Mode="NextPrevious"
    PagerSettings-Visible="true"
    PagerSettings-Position="Bottom"
    PagerStyle-BorderWidth="0"
    ShowFooter="true"
    Font-Size="Medium"
    DataSourceID="odsInventarios">

    <PagerSettings PreviousPageImageUrl="~/Images/previous.png" NextPageImageUrl="~/Images/next.png" />

    <Columns>
        <asp:TemplateField ItemStyle-Width="50">
            <ItemTemplate>

                <asp:ImageButton ID="ibtnShowLocalizacoes" runat="server" Width="15" OnClick="ibtnShowLocalizacoes_Click" ImageUrl="~/images/plus.png"
                    CommandArgument="Show" />
                <asp:Panel ID="pnlLocalizacoes" runat="server" Visible="false" Style="position: relative">

                    <asp:GridView
                        ID="gvLocalizacoes"
                        DataKeyNames="LocalizacaoID"
                        OnPageIndexChanging="gvLocalizacoes_PageIndexChanging"
                        runat="server"
                        AutoGenerateColumns="false"
                        PageSize="10"
                        BorderStyle="Solid"
                        BorderWidth="1px"
                        CellPadding="4"
                        GridLines="Both"
                        AllowPaging="true"
                        Width="800px"
                        ShowHeaderWhenEmpty="true"
                        EmptyDataText="Sem Registos"
                        PagerSettings-Mode="NextPrevious"
                        PagerSettings-Visible="true"
                        PagerSettings-Position="Bottom"
                        PagerStyle-BorderWidth="0"
                        Font-Size="Medium">

                        <PagerSettings PreviousPageImageUrl="~/Images/previous.png" NextPageImageUrl="~/Images/next.png" />
                        <Columns>
                            <asp:TemplateField ItemStyle-Width="50">
                                <ItemTemplate>

                                    <asp:ImageButton ID="ibtnShowEtiquetas" runat="server" Width="15" OnClick="ibtnShowEtiquetas_Click" ImageUrl="~/images/plus.png" CommandArgument="Show" />
                                    <asp:Panel ID="pnlEtiquetas" runat="server" Visible="false" Style="position: relative">

                                        <asp:GridView
                                            ID="gvEtiquetas"
                                            runat="server"
                                            AutoGenerateColumns="false"
                                            PageSize="10"
                                            ShowFooter="true"
                                            OnRowCommand="gvEtiquetas_RowCommand"
                                            AllowPaging="true"
                                            OnPageIndexChanging="gvEtiquetas_PageIndexChanging"
                                            ShowHeaderWhenEmpty="true"
                                            EmptyDataText="Sem Registos"
                                            Width="500px"
                                            PagerSettings-Mode="NextPrevious"
                                            PagerSettings-Visible="true"
                                            PagerSettings-Position="Bottom"
                                            PagerStyle-BorderWidth="0"
                                            Font-Size="Large">

                                            <PagerSettings PreviousPageImageUrl="~/Images/previous.png" NextPageImageUrl="~/Images/next.png" />
                                            <Columns>



                                                <asp:TemplateField HeaderText="Etiqueta">

                                                    <ItemTemplate>
                                                        <asp:Label ID="lblEtiqueta" runat="server" CssClass="badge badge-secondary" Text='<%# Eval("Etiqueta") %>'></asp:Label>
                                                    </ItemTemplate>

                                                    <FooterStyle BackColor="#020023" ForeColor="White" />
                                                    <FooterTemplate>

                                                        <asp:TextBox ID="txtEtiqueta" CssClass="form-control form-control-sm rounded border border-dark" runat="server" Text="" placeholder="Etiqueta..."></asp:TextBox>
                                                        <asp:RequiredFieldValidator ControlToValidate="txtEtiqueta" CssClass="server-validator" ErrorMessage="Campo necessário" ValidationGroup="vgEtiqueta" runat="server"></asp:RequiredFieldValidator>
                                                        <asp:RegularExpressionValidator ControlToValidate="txtEtiqueta" CssClass="server-validator" ErrorMessage="Insira um número válido." ValidationGroup="vgEtiqueta" runat="server"></asp:RegularExpressionValidator>

                                                    </FooterTemplate>

                                                </asp:TemplateField>

                                                <asp:TemplateField>
                                                    <FooterTemplate>
                                                        <asp:Button Text="Inserir" CommandName="Insert" runat="server" ID="btnAddEtiqueta" ValidationGroup="vgEtiqueta" />
                                                        <asp:Button Text="Cancel" CommandName="Cancel" runat="server" ID="btnCancel" />
                                                    </FooterTemplate>
                                                </asp:TemplateField>

                                            </Columns>
                                            <FooterStyle BackColor="#020023" ForeColor="#333333" />
                                            <HeaderStyle CssClass="HeaderStyle" BackColor="#020023" Font-Bold="True" ForeColor="White" />
                                            <AlternatingRowStyle CssClass="AlternatingRowStyle" />
                                            <PagerStyle BackColor="#020023" ForeColor="White" HorizontalAlign="Center" />
                                            <RowStyle BackColor="White" ForeColor="#333333" />
                                            <EmptyDataTemplate>
                                                <asp:TextBox ID="tbEmptyInsert" runat="server"></asp:TextBox><br />
                                                <asp:Button ID="btSend" Text="Insert" runat="server" CommandName="EmptyInsert" UseSubmitBehavior="False" />
                                            </EmptyDataTemplate>
                                        </asp:GridView>
                                    </asp:Panel>
                                </ItemTemplate>
                            </asp:TemplateField>

                            <asp:BoundField DataField="rowNumber" HeaderText="Nº" ReadOnly="True" ItemStyle-Font-Bold="true" SortExpression="LocalizacaoID" ItemStyle-Width="50" />
                            <asp:BoundField DataField="Localizacao" HeaderText="Localizacão" SortExpression="Localizacao" ItemStyle-Font-Bold="true" ItemStyle-ForeColor="#020023" />

                            <asp:TemplateField HeaderText="Etiq. Por Inventariar" SortExpression="EtiquetasPorInventariar">
                                <ItemTemplate>

                                    <asp:Label ID="lblEtiquetasPorInventariar" Font-Bold="true" CssClass="badge badge-warning border border-dark" Text='<%# Eval("EtiquetasPorInventariar") %>' runat="server"></asp:Label>

                                </ItemTemplate>
                            </asp:TemplateField>


                            <asp:TemplateField HeaderText="Etiq. Inventariadas" SortExpression="EtiquetasInventariadas">
                                <ItemTemplate>

                                    <asp:Label ID="lblEtiquetasInventariadas" Font-Bold="true" CssClass="badge badge-info border border-dark" Text='<%# Eval("EtiquetasInventariadas") %>' runat="server"></asp:Label>

                                </ItemTemplate>
                            </asp:TemplateField>

                            <asp:BoundField DataField="Precisao" HeaderText="Precisão (%)" SortExpression="Precisao" ItemStyle-Width="150" />

                        </Columns>

                        <FooterStyle BackColor="#020023" ForeColor="#333333" />
                        <HeaderStyle CssClass="HeaderStyle" BackColor="#020023" Font-Bold="True" ForeColor="White" />
                        <AlternatingRowStyle CssClass="AlternatingRowStyle" />
                        <PagerStyle BackColor="#020023" ForeColor="White" HorizontalAlign="Center" />
                        <RowStyle BackColor="White" ForeColor="#333333" />

                    </asp:GridView>
                </asp:Panel>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="InventarioID" HeaderText="Nº" ItemStyle-Font-Bold="true" InsertVisible="False" ReadOnly="True" SortExpression="InventarioID" ItemStyle-Width="50" />
        <asp:BoundField DataField="Nome" HeaderText="Colaborador" SortExpression="Colaborador" />
        <asp:BoundField DataField="Armazem" HeaderText="Armazém" SortExpression="Armazem" ItemStyle-Width="150" />

        <asp:TemplateField HeaderText="Estado" SortExpression="EstadoInventario" ItemStyle-Width="150">
            <ItemTemplate>
                <asp:Label ID="lblEstadoArtigo" runat="server" CssClass='<%# Convert.ToBoolean(Eval("EstadoInventario")) ? "badge badge-success" : "badge badge-danger" %>' Text='<%# Convert.ToBoolean(Eval("EstadoInventario")) ? "Aberto" : "Fechado" %>'>                                   
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:BoundField DataField="Precisao" HeaderText="Total (%)" SortExpression="Precisao" ItemStyle-Width="150" />
        <asp:BoundField DataField="DataCriacao" DataFormatString="{0:dd-MM-yyyy}" HeaderText="Data" SortExpression="DataCriacao" ItemStyle-Width="150" />
    </Columns>

    <FooterStyle BackColor="#020023" ForeColor="#333333" />
    <HeaderStyle CssClass="HeaderStyle" BackColor="#020023" Font-Bold="True" ForeColor="White" />
    <AlternatingRowStyle CssClass="AlternatingRowStyle" />
    <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Center" />
    <RowStyle BackColor="White" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F7F7F7" />
    <SortedAscendingHeaderStyle BackColor="#487575" />
    <SortedDescendingCellStyle BackColor="#E5E5E5" />
    <SortedDescendingHeaderStyle BackColor="#275353" />
</asp:GridView>

1 个答案:

答案 0 :(得分:0)

我会尝试通过LostFocus事件将其关闭。

Lost Focus Event

    private void DataGrid1_Validating(object sender, 
    private void DataGrid1_LostFocus(object sender, System.EventArgs e)
{
    // Close datagrid here
}