嵌套gridview展开折叠不起作用

时间:2015-05-04 06:32:10

标签: c# asp.net gridview

您好我将此代码用于嵌套网格视图,并且嵌套网格视图没有错误。当单击一行加上图像时,其他行无法自动隐藏。任何人都可以帮忙...

<script language="javascript" type="text/javascript">
    function divexpandcollapsenew(divname) {

        var div = document.getElementById(divname);
        var img = document.getElementById('imgdiv' + divname);
        if (div.style.display == "none") {
            div.style.display = "inline";
            img.src = "images/minus.gif";
        } else {
            div.style.display = "none";
            img.src = "images/plus.gif";
        }
    }
</script>

<script type="text/javascript">

    $(document).ready(function () {

        $("#imgBtnClose").click(function (e) {
            HideDialog();
            e.preventDefault();
        });
    });


    function HideDialog() {
        $("#overlay").hide();
        $("#dialog").fadeOut(100);
    }

</script>

标记

<div style="background-image: url(images/bgMW.png);">
    <hr />
    <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
        <ContentTemplate>
            <fieldset style="border: none" title="Time Data">
                <div align="center">
                    <asp:Label ID="lblHead" runat="server" class="Heading1" Text="Time Data"></asp:Label>
                </div>
                <br />
                <div align="center">
            </fieldset>
            <div id="all" class="divassigngrid">
                <div align="left">
                    <asp:Label ID="lblInfo" Text="Time Data Details" runat="server" CssClass="Heading2"></asp:Label>
                </div>
                <br />
                <div align="left" style="width: 778px; margin-left: 0px">
                    <asp:Label ID="Label1" runat="server" CssClass="lblErr" Text="From"></asp:Label>
                    <asp:TextBox ID="txtFromDate" Width="66px" runat="server" CssClass="textbox" onkeypress="return false"
                        OnTextChanged="txtFromDate_TextChanged" AutoPostBack="true"></asp:TextBox>
                    <asp:Label ID="Label2" runat="server" CssClass="lblErr" Text="To"></asp:Label>
                    <asp:TextBox ID="txtToDate" Width="66px" runat="server" CssClass="textbox" AutoPostBack="true" onkeypress="return false"
                        OnTextChanged="txtToDate_TextChanged"></asp:TextBox>
                    <asp:Label ID="Label3" runat="server" CssClass="lblErr" Text="Weekly Off"></asp:Label>
                    <asp:Label ID="Label4" runat="server" CssClass="lblErr" Text="Toooooo" ForeColor="Bisque" BackColor="Bisque"></asp:Label>
                    <asp:Label ID="Label5" runat="server" CssClass="lblErr" Text="Mismatch"></asp:Label>
                    <asp:Label ID="Label6" runat="server" CssClass="lblErr" Text="Toooooo" BackColor="LightPink" ForeColor="LightPink"></asp:Label>
                    <asp:Label ID="lblerr" runat="server" Font-Bold="True" Text="" Font-Size="Medium" Style="margin-left: 10px"></asp:Label>
                </div>
                <br />
                <div align="left">
                    <asp:GridView ID="gvAttendance" Width="100%" AutoGenerateColumns="False" runat="server" CssClass="grid"
                        EmptyDataText="No Records Found." CellPadding="4" GridLines="Both" SelectedRowStyle-BackColor="#FFFFCC"
                        SelectedRowStyle-CssClass="GV1" OnRowCreated="gvAttendance_RowCreated" OnSelectedIndexChanged="gvAttendance_SelectedIndexChanged"
                        OnRowCommand="gvAttendance_RowCommand" OnRowDataBound="gvAttendance_RowDataBound" OnRowUpdating="gvAttendance_RowUpdating">
                        <FooterStyle CssClass="GV2" />
                        <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
                        <HeaderStyle CssClass="GV1" />
                        <%--<RowStyle CssClass="GV2" />--%>
                        <Columns>
                            <asp:TemplateField HeaderImageUrl="images/Icon_HeaderLock.jpg">
                                <ItemTemplate>
                                    <%-- <asp:Label ID="lblLock" runat="server" CssClass="lblErr" Text="Lock"></asp:Label>--%>
                                    <asp:Image ID="iLock" ImageUrl="images/Icon_HeaderLock.jpg" ToolTip="Lock" Width="12px" Height="12px"
                                        runat="server" />
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderImageUrl="images/Icon_fullday.Jpg">
                                <ItemTemplate>
                                    <%--<asp:Label ID="lblLeave" runat="server" CssClass="lblErr" Text="Leave"></asp:Label>--%>
                                    <asp:Image ID="iLeave" ImageUrl="images/Icon_fullday.Jpg" ToolTip="FullDay" Width="12px" Height="12px"
                                        runat="server" />
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderImageUrl="images/Icon_Error.Jpg">
                                <ItemTemplate>
                                    <%--<asp:Label ID="lblError" runat="server" CssClass="lblErr" Text="Error"></asp:Label>--%>
                                    <asp:Image ID="iError" ImageUrl="images/Icon_Error.jpg" Width="12px" Height="12px" runat="server" ToolTip="Error" />
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                            </asp:TemplateField>
                            <asp:TemplateField ItemStyle-Width="20px">
                                <ItemTemplate>
                                    <a href="JavaScript:divexpandcollapsenew('div<%# Eval("Date") %>');">
                                        <img alt="childimg" id="imgdiv<%# Eval("Date")%>"  width="12px" height="12px" border="0" src="images/plus.gif" / >
                                    </a>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Date">
                                <ItemTemplate>
                                    <asp:Label ID="lblActDate" runat="server" CssClass="lblErr" Text='<%# Eval("Date")%>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Shift">
                                <ItemTemplate>
                                    <asp:Label ID="lblShiftCode" runat="server" CssClass="lblErr" Text='<% # Eval("Shift") %>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Day">
                                <ItemTemplate>
                                    <asp:Label ID="lblDay" runat="server" CssClass="lblErr" Text='<% # Eval("Day") %>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Status">
                                <ItemTemplate>
                                    <asp:Label ID="lblShiftStatus" runat="server" CssClass="lblErr" Text='<%# Eval("Status")%>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="First In">
                                <ItemTemplate>
                                    <asp:Label ID="lblFirstIn" runat="server" CssClass="lblErr" Text='<% # Eval("First In") %>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Last Out">
                                <ItemTemplate>
                                    <asp:Label ID="lblLastOut" runat="server" CssClass="lblErr" Text='<%# Eval("Last Out")%>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Eff. Hrs (H)">
                                <ItemTemplate>
                                    <asp:Label ID="lblEffHrs" runat="server" CssClass="lblErr" Text='<% # Eval("Eff") %>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Late (M)">
                                <ItemTemplate>
                                    <asp:Label ID="lblLate" runat="server" CssClass="lblErr" Text='<%# Eval("Late")%>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Early (M)">
                                <ItemTemplate>
                                    <asp:Label ID="lblEarly" runat="server" CssClass="lblErr" Text='<% # Eval("Early") %>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Permission (M)">
                                <ItemTemplate>
                                    <asp:Label ID="lblPermission" runat="server" CssClass="lblErr" Text='<% # Eval("Permission") %>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Action">
                                <ItemTemplate>
                                    <asp:ImageButton ID="IBAction" runat="server" Width="12px" Height="12px" ImageUrl="~/images/AddInsideGV.png"
                                        CausesValidation="false" ToolTip="Add" CommandName="additem" CommandArgument="Date" />
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                            </asp:TemplateField>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <tr>
                                        <td colspan="100%">
                                            <div id="div<%# Eval("Date") %>" style="display: none; position: relative; left: 15px; overflow: auto">
                                                <asp:GridView ID="gvChildGrid" Width="97%" runat="server" AutoGenerateColumns="false" CssClass="grid"
                                                    EmptyDataText="No Records Found." CellPadding="4" GridLines="Both" OnRowCommand="gvChildGrid_RowCommand"
                                                    OnRowEditing="gvChildGrid_OnRowEditing" OnRowUpdating="gvChildGrid_RowUpdating" SelectedRowStyle-BackColor="#FFFFCC"
                                                    SelectedRowStyle-CssClass="GV1">
                                                    <FooterStyle CssClass="GV2" />
                                                    <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
                                                    <HeaderStyle CssClass="GV1" />
                                                    <RowStyle CssClass="GV2" />
                                                    <Columns>
                                                        <asp:TemplateField HeaderText="Date">
                                                            <ItemTemplate>
                                                                <asp:Label ID="lblSwipeDate" runat="server" CssClass="lblErr" Text='<%# Eval("Date")%>'></asp:Label>
                                                            </ItemTemplate>
                                                            <ItemStyle HorizontalAlign="Center" />
                                                        </asp:TemplateField>
                                                        <asp:TemplateField HeaderText="Swipe Time">
                                                            <ItemTemplate>
                                                                <asp:Label ID="lblSwipteTime" runat="server" CssClass="lblErr" Text='<% # Eval("Swipe Time") %>'></asp:Label>
                                                            </ItemTemplate>
                                                            <ItemStyle HorizontalAlign="Center" />
                                                        </asp:TemplateField>
                                                        <asp:TemplateField HeaderText="Mode">
                                                            <ItemTemplate>
                                                                <asp:Label ID="lblMode" runat="server" CssClass="lblErr" Text='<%# Eval("Mode")%>'></asp:Label>
                                                            </ItemTemplate>
                                                            <ItemStyle HorizontalAlign="Center" />
                                                        </asp:TemplateField>
                                                        <asp:TemplateField HeaderText="Change To">
                                                            <ItemTemplate>
                                                                <asp:DropDownList ID="ddlChangeTo" runat="server" CssClass="ddl">
                                                                    <asp:ListItem Value="IN">IN</asp:ListItem>
                                                                    <asp:ListItem Value="OUT">OUT</asp:ListItem>
                                                                    <asp:ListItem Value="DELETE">DELETE</asp:ListItem>
                                                                </asp:DropDownList>
                                                            </ItemTemplate>
                                                            <ItemStyle HorizontalAlign="Center" />
                                                        </asp:TemplateField>
                                                        <asp:TemplateField HeaderText="Status">
                                                            <ItemTemplate>
                                                                <asp:Label ID="lblStatus" runat="server" CssClass="lbl"></asp:Label>
                                                            </ItemTemplate>
                                                            <ItemStyle HorizontalAlign="Center" />
                                                        </asp:TemplateField>
                                                        <asp:TemplateField HeaderText="Remarks">
                                                            <ItemTemplate>
                                                                <asp:DropDownList ID="ddlRemarks" runat="server" CssClass="ddl">
                                                                    <asp:ListItem Value="0">---Select---</asp:ListItem>
                                                                    <asp:ListItem Value="1">Wrong Time Data Mode Selected ( In / Out )</asp:ListItem>
                                                                    <asp:ListItem Value="2">Multiple Time Data Entry</asp:ListItem>
                                                                    <asp:ListItem Value="3">Multiple Time Data Entry - System Error</asp:ListItem>
                                                                </asp:DropDownList>
                                                            </ItemTemplate>
                                                            <ItemStyle HorizontalAlign="Center" />
                                                        </asp:TemplateField>
                                                        <asp:TemplateField HeaderText="Action">
                                                            <ItemTemplate>
                                                                <asp:ImageButton ID="IBActionInsideGV" runat="server" Width="12px" Height="12px" ImageUrl="~/images/Ratify.gif"
                                                                    CausesValidation="false" ToolTip="Ratify" OnClientClick="return confirm('Do you want to Ratify?')"
                                                                    CommandArgument='<%#((GridViewRow)Container).RowIndex %>' CommandName="RatifyForm" />
                                                            </ItemTemplate>
                                                            <ItemStyle HorizontalAlign="Center" />
                                                        </asp:TemplateField>
                                                    </Columns>
                                                </asp:GridView>
                                            </div>
                                        </td>
                                    </tr>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </div>

这是我的后端

protected void gvAttendance_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        for (int i = 0; i < gvAttendance.Columns.Count; i++)
        {
            e.Row.Cells[i].ToolTip = gvAttendance.Columns[i].HeaderText;
        }
        e.Row.Cells[0].ToolTip = "Lock";
        e.Row.Cells[1].ToolTip = "Leave";
        e.Row.Cells[2].ToolTip = "Error";
    }
    try
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblActDate = (Label)e.Row.FindControl("lblActDate");
            string actDate = lblActDate.Text;

            GridView gv = (GridView)e.Row.FindControl("gvChildGrid");

            ////GridViewRow row = (GridViewRow)gv.Parent.Parent;
            ////int idx = row.RowIndex;
            //////grid.Rows[myRow.RowIndex]
            ////Label ldate = (Label)gv.Rows[idx].FindControl("lblSwipeDate");
            gv.DataSource = sHandler.getChildAttendance(Convert.ToString(empCode), actDate);
            gv.DataBind();

            Label lblShiftStatus = (Label)e.Row.FindControl("lblShiftStatus");
            string day = lblShiftStatus.Text.Trim();
            if (day == "HOLIDAY")
            {
                e.Row.BackColor = System.Drawing.Color.Bisque;

            }
            Image iError = (Image)e.Row.FindControl("iError");
            iError.Visible = false;

            Label lblFirstIn = (Label)e.Row.FindControl("lblFirstIn");
            string firstIn = lblFirstIn.Text.Trim();
            if (firstIn == "")
            {
                e.Row.BackColor = System.Drawing.Color.LightPink;
                iError.Visible = true;

            }

            Label lblLastOut = (Label)e.Row.FindControl("lblLastOut");
            string lastOut = lblLastOut.Text.Trim();
            if (lastOut == "")
            {
                e.Row.BackColor = System.Drawing.Color.LightPink;
                iError.Visible = true;

            }

            if (firstIn == "" && lastOut == "")
            {
                e.Row.BackColor = System.Drawing.Color.Bisque;
                iError.Visible = true;
            }

            DateTime dtActDate = Convert.ToDateTime(actDate);
            DateTime firstDayOfTheMonth = new DateTime(dtActDate.Year, dtActDate.Month, 1);

            DataTable dtLockProcess = sHandler.getLockProcess(Convert.ToString(firstDayOfTheMonth));
            Image iLock = (Image)e.Row.FindControl("iLock");
            ImageButton IBAction = (ImageButton)e.Row.FindControl("IBAction");
            Image imgdiv = (Image)e.Row.FindControl("img");

            if (dtLockProcess.Rows.Count > 0)
            {
                if (dtLockProcess.Rows[0][0].ToString() == "Y")
                {
                    iLock.Visible = true;
                    IBAction.Visible = false;
                    //imgdiv.Visible = false;
                }
                else
                {
                    iLock.Visible = false;
                    IBAction.Visible = true;
                    //imgdiv.Visible = true;
                }

            }


            Label lblPermission = (Label)e.Row.FindControl("lblPermission");
            if (lblPermission.Text == "0")
            {
                lblPermission.Enabled = false;
            }
            else
            {
                lblPermission.Enabled = true;
            }

            Image iLeave = (Image)e.Row.FindControl("iLeave");
            iLeave.Visible = false;
            DataTable dtLeave = sHandler.getLeave(Convert.ToString(empCode), actDate);
            if (dtLeave.Rows.Count > 0)
            {
                if (dtLeave.Rows[0][0].ToString() == "M" || dtLeave.Rows[0][0].ToString() == "A")
                {
                    iLeave.ImageUrl = "images/Icon_forenoon.Jpg";
                    iLeave.Visible = true;
                }
                if (dtLeave.Rows[0][0].ToString() == "N")
                {
                    iLeave.ImageUrl = "images/Icon_fullday.Jpg";
                    iLeave.Visible = true;
                    e.Row.BackColor = System.Drawing.Color.LightPink;

                }
            }

        }
    }
    catch
    {

    }
}

0 个答案:

没有答案