GridView 不允许找到任何控件

Posted

技术标签:

【中文标题】GridView 不允许找到任何控件【英文标题】:GridView is not allowing to find any control 【发布时间】:2013-11-20 04:46:03 【问题描述】:

我想使用嵌套的gridview。但是我的父网格在 ChildGrid 的变量中显示Nothing

有什么帮助吗?我尝试了以下方法:

1)

将 gvChild 调暗为 GridView = CType(Me.gvwSubjectForProject.FindControl("gvChildGrid"), GridView)

2)

将 gvChild 调暗为 GridView = Me.gvwSubjectForProject.FindControl("gvChildGrid") gvChild.DataSource = dt_Subject gvChild.DataBind()

我想找到子网格的id,然后想给它分配数据源。

VB

Private Function FillGrid(ByVal mode As String, ByVal dt_Subject As 数据表)作为布尔值 '将 dt_Subject 作为 DataTable = 无 'Dim dt_sortsubject As DataTable = 无 试试

        If (mode = "ParentGrid") Then
            Me.gvwSubjectForProject.DataSource = Nothing
            Me.gvwSubjectForProject.DataBind()
            'Me.ViewState(VS_CurrentSubject) = Nothing
            Me.btnExport.Visible = False

            'dt_Subject.Columns.Add("Status", GetType(String))
            If Not dt_Subject Is Nothing Then
                If dt_Subject.Rows.Count > 0 Then
                    If dt_Subject.Rows.Count > 0 Then
                        For Each dr_Row In dt_Subject.Rows
                            If dr_Row("cStatus") = "AC" Then
                                dr_Row("Status") = "Active"
                            ElseIf dr_Row("cStatus") = "IA" Then
                                dr_Row("Status") = "In Active"
                            ElseIf dr_Row("cStatus") = "HO" Then
                                dr_Row("Status") = "Hold"
                            ElseIf dr_Row("cStatus") = "SC" Then
                                dr_Row("Status") = "Screened"
                            ElseIf dr_Row("cStatus") = "BO" Then
                                dr_Row("Status") = "Booked"
                            ElseIf dr_Row("cStatus") = "OS" Then
                                dr_Row("Status") = "On Study"
                            ElseIf dr_Row("cStatus") = "FI" Then
                                dr_Row("Status") = "Forever Ineligible"
                            Else
                                dr_Row("cStatus") = "Not Found"
                            End If
                        Next
                        Me.btnExport.Visible = True
                        'Me.ViewState(VS_CurrentSubject) = dt_Subject
                        Me.gvwSubjectForProject.DataSource = dt_Subject
                        Me.gvwSubjectForProject.DataBind()
                    End If
                End If
            End If

        ElseIf (mode = "ChildGrid") Then
            If Not dt_Subject Is Nothing Then
                If dt_Subject.Rows.Count > 0 Then

                    'Me.gvwSubjectForProject.DataSource = dt_Subject
                    'Me.gvwSubjectForProject.DataBind()

                    'Me.gvwSubjectForProject.FindControl("gvChildGrid").DataSource =

dt_Subject

                    'CType(Me.gvwSubjectForProject.FindControl("gvChildGrid"),

GridView).DataBind() '我.gvwSubjectForProject.FindControl("gvChildGrid")。 'Me.gvwSubjectForProject.FindControl("gvChildGrid").DataBind()

                    Dim gvChild As GridView = Me.gvwSubjectForProject.FindControl("gvChildGrid")
                    gvChild.DataSource = dt_Subject
                    gvChild.DataBind()
                End If
            End If
        End If

        Return True

    Catch ex As Exception
        Me.ShowErrorMessage("Error While Binding Grid", "....FillGrid")
        Return False
    End Try
End Function

html 嵌套网格视图

 <asp:Panel ID="pnlgvwSubjectStatus" runat="server" Style="max-height: 500px; overflow: auto;
            width: 60%;">
            <asp:GridView ID="gvwSubjectForProject" runat="server" AutoGenerateColumns="false"
                SkinID="grdViewSmlAutoSize" Width="70%" ShowFooter="true" AllowPaging="true"
                PageSize="1">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:ImageButton ID="imgbtnExpand" runat="server" ImageUrl="../images/Plus.gif" 
                                OnClientClick="imgbtnExpand_Click" />
                            <asp:HiddenField ID="hcStatus" runat="server" Value='<%# Eval("cStatus") %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="Status" HeaderText="STATUS">
                        <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Wrap="true" Width="40%" />
                        <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="true" Width="40%" />
                    </asp:BoundField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <div style="max-height: 300px; width: 100%; overflow: auto;
                                text-align: right;">
                                <%--<asp:Panel ID="pnlOrders" runat="server" Width="100%">--%>
                                    <asp:GridView ID="gvChildGrid" runat="server" AutoGenerateColumns="false" SkinID="grdViewSmlAutoSize">
                                        <Columns>
                                            <asp:BoundField ItemStyle-Width="40%" DataField="vSubjectID" HeaderText="SUBJECT ID">
                                                <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Wrap="true" Width="40%" />
                                                <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="true" Width="40%" />
                                            </asp:BoundField>
                                            <asp:BoundField ItemStyle-Width="60%" DataField="FullName" HeaderText="FULL NAME">
                                                <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Wrap="true" Width="60%" />
                                                <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="true" Width="60%" />
                                            </asp:BoundField>
                                        </Columns>
                                    </asp:GridView>
                                <%--</asp:Panel>--%>
                            </div>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="TotalSubject" HeaderText="TOTAL SUBJECTS">
                        <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Wrap="true" Width="50%" />
                        <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="true" Width="50%" />
                    </asp:BoundField>
                </Columns>
            </asp:GridView>
        </asp:Panel>

还有一件事,在 HTML 中,当子 gridview 不显示时,它仍然在创建它的 DOM 时填充。我也想删除那个空白。

【问题讨论】:

请发布您正在执行这些尝试的FindControl() 调用的事件方法的所有代码。另外,能否请您发布嵌套网格视图控件的标记? 请检查更新的代码。 【参考方案1】:

您发布的代码不起作用,因为您在整个父网格视图控件上使用FindControl() 方法,而不是在网格视图控件中的单个行上,如下所示:

OnRowDataBound 事件中查找子网格视图:

Sub ParentGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    ' Only look for child grid view in data rows, ignore header and footer rows
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim theChildGridView As GridView = DirectCast(e.Row.FindControl("gvChildGrid"), GridView)

        ' Do something with theChildGridView here

    End If
End Sub

通过遍历父网格视图控件中的所有行来查找子网格视图:

For Each row As GridViewRow In ParentGridView.Rows
    ' Only look for child grid view in data rows, ignore header and footer rows
    If row.RowType = DataControlRowType.DataRow Then
        Dim theChildGridView As GridView = DirectCast(row.FindControl("gvChildGrid"), GridView)

        ' Do something with theChildGridView here
    End If
Next

【讨论】:

但至少应该有任何方法可以控制 Parent_gridview 中的 Child_gridview ...即使在调试时也找不到 imgbtnExpand。 好的。我会尝试这种方式,但与此同时,您能帮我解决我在问题最后一句中提到的其他问题吗? 问题似乎在 .....CHILD GRID..... 'cz 即使没有获取子网格,模板字段也是转化为

以上是关于GridView 不允许找到任何控件的主要内容,如果未能解决你的问题,请参考以下文章

asp.net c# gridview的居中问题

如何在 GridView 的 TemplateField 中找到控件?

c#winfrom中gridview控件怎样设置让表头不可点击,还有下面点击全选中,还不是点击只显示一个单元格,求助,谢

Android入门第八篇之GridView(九宫图)

Android入门第八篇之GridView(九宫图)

dropdownlist控件下有的项不允许选择可以吗?