【问题标题】:How to find the nested Asp:GridView within the following code?如何在以下代码中找到嵌套的 Asp:GridView?
【发布时间】:2009-03-30 19:37:38
【问题描述】:

我在一个 aspx 页面中有以下结构:

<asp:Panel ID="pnlCust" runat="server">
    <asp:GridView ID="gvMaster" runat="server" 
                  OnRowCreated="gvMaster_RowCreated">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Panel ID="pnlMaster" runat="server">
                        //...
                    </asp:Panel>
                    <asp:Panel ID="pnlDetails" runat="server">
                        <asp:GridView ID="gvDetails" runat="server">
                            <Columns>
                                //...
                            </Columns>
                        </asp:GridView>
                    </asp:Panel>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</asp:Panel>

面板用于 Ajax 控件工具包中的 CollapsiblePanelExtender。

我正在尝试使用 FindControl 在代码隐藏中查找 gvDetails 控件,但我最近的尝试没有奏效,即在 gvMaster_RowCreated 事件中:

GridView gv =  
e.Row.FindControl("pnlDetails").FindControl("gvDetails") as GridView;

其中 e 是 GridViewRowEventArgs

我基本上是在做这个页面上的内容,但我没有使用 SqlDataSource,但是,这个人基本上是通过 FindControl 从 RowCreated 事件传入的 e 参数中找到 SqlDataSource。这是链接:

http://mosesofegypt.net/post/2008/02/Building-a-grouping-Grid-with-GridView-and-ASPNET-AJAX-toolkit-CollapsiblePanel.aspx

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    这是一个对我有用的代码隐藏方法:

    protected void gvMaster_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow) {
            GridView gv = (GridView)e.Row.FindControl("gvDetails");
        }
    }
    

    您的应用是否抛出异常?什么不正常?

    【讨论】:

    • 我试过了,就像你在上面一样,但我按照以下方式返回了一个空值:GridView gv = e.Row.FindControl("gvDetails") as GridView;
    • 我在上面试过你的sn-p,当我单步执行时,gv 为空。
    • 我想我被难住了。对不起!我唯一想尝试的是使用 RowDataBound 事件而不是 RowCreated ,但这可能是一个很长的镜头。
    • 内特,对不起,你是对的。愚蠢的我在标记中将 GridView ID 拼写为 gvDtails,所以这就是它没有找到它的原因。谢谢!
    【解决方案2】:

    我不相信 GridView 可以有这样的子控件。

    【讨论】:

    • 你说的是哪个子控件?
    【解决方案3】:

    您需要将面板放在模板字段中。你可以试试这样:

    <asp:Panel ID="pnlCust" runat="server"> 
        <asp:GridView ID="gvMaster" runat="server" >
            <columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Panel ID="pnlDetails" runat="server">
                            <asp:GridView ID="gvDtails" runat="server">
                                <columns>
                                    <asp:TemplateField>
                                        <ItemTemplate>
                                            <%-- columns here --%>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </columns>
                            </asp:GridView> <%-- end gvDetails --%>
                        </asp:Panel> <%-- end pnlDetails--%>
                    </ItemTemplate>
                </asp:TemplateField>
            </columns>
        </asp:GridView> <%-- end gvMaster --%>
    </asp:Panel> <%-- end pnlCust --%>
    

    【讨论】:

    • 是的,代码实际上是这样的。为了更清楚,我只是把它拿出来,现在我试图在 Code Behind 中找到 gvDetails GridView。我将使用模板字段更新我的问题。
    猜你喜欢
    • 2012-10-09
    • 2022-01-09
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 2021-12-05
    • 2017-02-09
    • 2019-06-08
    • 1970-01-01
    相关资源
    最近更新 更多