如何访问嵌套的GridView

时间:2015-04-05 05:49:54

标签: c# asp.net gridview nested

Tere是 gridview (嵌套)的四个级别,一切正常但是对于" gvEmployees "

我有权访问"gvDepartments", "gvFuctions" & "gvSections" 我如何访问" gvEmployees "?

<asp:GridView ID="gvDepartments" runat="server" AutoGenerateColumns="false" CssClass="Grid" Width="100%" DataKeyNames="EMPLOYEE_NUMBER">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:ImageButton ID="imgFunctionShow" runat="server" OnClick="Show_Hide_FunctionsGrid" ImageUrl="~/images/plus.png" CommandArgument="Show" />
                    <asp:Panel ID="pnlFunctions" runat="server" Visible="false" Style="position: relative">
                        <asp:GridView ID="gvFunctions" runat="server" AutoGenerateColumns="false" PageSize="10" Width="100%" AllowPaging="true" 
                            OnPageIndexChanging="OnFunctionsGrid_PageIndexChanging" CssClass="ChildGrid" DataKeyNames="EMPLOYEE_NUMBER">
                            <Columns>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:ImageButton ID="imgSectionsShow" runat="server" OnClick="Show_Hide_SectionsGrid" ImageUrl="~/images/plus.png" CommandArgument="Show" />
                                        <asp:Panel ID="pnlSections" runat="server" Visible="false" Style="position: relative">
                                            <asp:GridView ID="gvSections" runat="server" AutoGenerateColumns="false" PageSize="10" Width="100%"
                                                AllowPaging="true" OnPageIndexChanging="OnSectionsGrid_PageIndexChanging">
                                                <Columns>
                                                    <asp:TemplateField>
                                                        <ItemTemplate>
                                                            <asp:ImageButton ID="imgEmployeesShow" runat="server" OnClick="Show_Hide_EmployeesGrid" ImageUrl="~/images/plus.png" CommandArgument="Show" />
                                                            <asp:Panel ID="pnlEmployees" runat="server" Visible="false" Style="position: relative">
                                                                <asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="false" PageSize="10" Width="100%"
                                                                    AllowPaging="true" OnPageIndexChanging="OnEmployeesGrid_PageIndexChanging" >
                                                                    <Columns>
                                                                        <asp:BoundField DataField="EMPLOYEE_NUMBER" HeaderText="Employee#" />
                                                                        <asp:BoundField DataField="EMPNAMEE" HeaderText="Employee Name" />
                                                                        <asp:BoundField DataField="Position" HeaderText="Position" />  
                                                                    </Columns>
                                                                </asp:GridView>
                                                            </asp:Panel>
                                                         </ItemTemplate>
                                                    </asp:TemplateField>
                                                    <asp:BoundField DataField="EMPLOYEE_NUMBER" HeaderText="Employee#" />
                                                    <asp:BoundField DataField="EMPNAMEE" HeaderText="Employee Name" />
                                                    <asp:BoundField DataField="Section" HeaderText="Section" />
                                                    <asp:BoundField DataField="Position" HeaderText="Position" />
                                                    <asp:BoundField DataField="SVName" HeaderText="Reporting To" />
                                                </Columns>
                                            </asp:GridView>
                                        </asp:Panel>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:BoundField DataField="EMPLOYEE_NUMBER" HeaderText="Employee#" />
                                <asp:BoundField DataField="EMPNAMEE" HeaderText="Employee Name" />
                                <asp:BoundField DataField="FunctionName" HeaderText="Function Name" />
                                <asp:BoundField DataField="Position" HeaderText="Position" />
                                <asp:BoundField DataField="SVName" HeaderText="Reporting To" />
                            </Columns>
                        </asp:GridView>
                    </asp:Panel>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="EMPLOYEE_NUMBER" HeaderText="Employee#" />
            <asp:BoundField DataField="EMPNAMEE" HeaderText="Employee Name" />
            <asp:BoundField DataField="Department" HeaderText="Department" />
            <asp:BoundField DataField="Position" HeaderText="Position" />
            <asp:BoundField DataField="LocInfo" HeaderText="Location" />
            <asp:BoundField DataField="SVName" HeaderText="Reporting To" />
        </Columns>
    </asp:GridView>

C#代码:

 protected void Show_Hide_EmployeesGrid(object sender, EventArgs e)
{
    ImageButton imgShowHide = (sender as ImageButton);
    GridViewRow row = (imgShowHide.NamingContainer as GridViewRow);
    if (imgShowHide.CommandArgument == "Show")
    {
        row.FindControl("pnlEmployees").Visible = true;
        imgShowHide.CommandArgument = "Hide";
        imgShowHide.ImageUrl = "~/images/minus.png";
        string empId =  (row.NamingContainer as GridView).DataKeys[row.RowIndex].Value.ToString();             
        GridView gvEmployees = row.FindControl("gvEmployees") as GridView;
        BindEmployees(empId, gvEmployees);  
        //gvEmployees.
    }
    else
    {
        row.FindControl("pnlEmployees").Visible = false;
        imgShowHide.CommandArgument = "Show";
        imgShowHide.ImageUrl = "~/images/plus.png";
    }
}

private void BindEmployees(string empId, GridView gvEmployees)
{
    gvEmployees.ToolTip = empId.ToString();
    gvEmployees.DataSource = GetData(string.Format("SELECT empinfo_v_update.EMPLOYEE_NUMBER, empinfo_v_update.EMPNAMEE, empinfo_v_update.POSITION, empinfo_v_update.FunctionName, " +
                        " empinfo_v_update.UserLevel, UserLevel.Description, empinfo_v_update.Section, 0 AS EmployeeLevel, " +
                        " empinfo_v_update_1.EMPNAMEE AS SVName " +
                        " FROM empinfo_v_update INNER JOIN " +
                        " UserLevel ON empinfo_v_update.UserLevel = UserLevel.UserLevel INNER JOIN " +
                        " empinfo_v_update AS empinfo_v_update_1 ON " +
                        " empinfo_v_update.SupervisorID = empinfo_v_update_1.EMPLOYEE_NUMBER " +
                        " WHERE (empinfo_v_update.SupervisorID ='{0}') ORDER BY LEN(empinfo_v_update.EMPLOYEE_NUMBER),empinfo_v_update.EMPLOYEE_NUMBER", empId));

    gvEmployees.DataBind();
}

protected void OnEmployeesGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView gvEmployees = (sender as GridView);
    gvEmployees.PageIndex = e.NewPageIndex;
    BindEmployees(gvEmployees.ToolTip, gvEmployees);
}

我在C#中尝试了下面的代码:

1 个答案:

答案 0 :(得分:0)

有一个简单的example,它适用于我,