GridView不允许查找任何控件

时间:2013-11-20 04:46:04

标签: html vb.net gridview nested

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

有任何帮助吗?我试过以下方法:

1)

  

Dim gvChild As GridView =   CType(Me.gvwSubjectForProject.FindControl(“gvChildGrid”),GridView)

2)

  

Dim gvChild As GridView =   Me.gvwSubjectForProject.FindControl( “gvChildGrid”)                           gvChild.DataSource = dt_Subject                           gvChild.DataBind()

我想找到Child grid的id,然后想要为它分配数据源。

VB

  

私有函数FillGrid(ByVal模式为String,ByVal dt_Subject As   DataTable)作为布尔值           'Dim dt_Subject As DataTable = Nothing           'Dim dt_sortsubject As DataTable = Nothing           尝试

        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()                           “Me.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 NESTED GRIDVIEW

 <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" alt="Expand"
                                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时仍然填充。我也想删除那个空格。

1 个答案:

答案 0 :(得分:0)

您发布的代码无效,因为您在整个父网格视图控件上使用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