在 aspx 页面中加载不同的 ascx

Load different ascx's in aspx page

在我的 aspx 页面上,我在 TreeView 中的每个节点的左侧都有 TreeView 我想加载一个不同的 ascx,它只会改变页面的右侧。

这就是我的 aspx.vb 的样子:

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    RetrieveAllQueryStringParams()

    If Not IsPostBack Then
        GenerateDataSets()
        GenerateLabels()
        GenerateLinks()
        GenerateControls()
        GenerateOther()
    End If

End Sub

当它到达 GenerateLinks() 时

 Protected Sub GenerateLinks()
        Dim subTreeStatisticalReport As RadTreeNode = rtvReports.Nodes.FindNodeByValue("StatisticalReport")
        Dim subTreeIncidentComparisons As RadTreeNode = rtvReports.Nodes.FindNodeByValue("IncidentComparisons")
        Dim subTreeGeographicLocations As RadTreeNode = rtvReports.Nodes.FindNodeByValue("GeographicLocations")

        Dim statTimeOfDayNode As RadTreeNode = subTreeStatisticalReport.Nodes.FindNodeByValue("TimeofDay")
        Dim statTrendsNode As RadTreeNode = subTreeStatisticalReport.Nodes.FindNodeByValue("Trends")
        Dim statTopSevenIncidents As RadTreeNode = subTreeStatisticalReport.Nodes.FindNodeByValue("Top7Incidents")
        Dim statIncidentPerCategory As RadTreeNode = subTreeStatisticalReport.Nodes.FindNodeByValue("IncidentsPerCategory")
        Dim compIncidentLoss As RadTreeNode = subTreeIncidentComparisons.Nodes.FindNodeByValue("IncidentLoss")
        Dim GeoMaps As RadTreeNode = subTreeGeographicLocations.Nodes.FindNodeByValue("Maps")




        statTimeOfDayNode.NavigateUrl = Globals.gRootRelativeSecureURL("/Reports/Reports.aspx?SessionID=" + m_SessionID + "&qrptTOD=tod")
End Sub

现在,如果用户要单击时间节点,它将再次加载相同的页面,但 ascx 不同。

Private Sub RetrieveAllQueryStringParams()
        Try
            If Not String.IsNullOrEmpty(Request.QueryString("SessionID")) Then
                m_SessionID = Request.QueryString("SessionID")
                m_qrptTOD = Request.QueryString("qrptTOD")
            ElseIf Not String.IsNullOrEmpty(Session("SessionID")) Then
                m_SessionID = Session("SessionID")
            End If

            If m_qrptTOD = "tod" Then
                Page.LoadControl("/Controls/Reports/rptTimeOfDay.ascx")
            End If


        Catch ex As Exception

        End Try
    End Sub

到达这里的时间:

 If m_qrptTOD = "tod" Then
                Page.LoadControl("/Controls/Reports/rptTimeOfDay.ascx")
            End If

ascx 页面由于某种原因没有加载!! 我做错了什么?!?!?

您正试图在页面加载事件中加载用户控件。太晚了。阅读有关 Asp.Net Page Life Cycle 的更多信息。相关部分引用如下。

Page Event Init: Raised after all controls have been initialized and any skin settings have been applied. The Init event of individual controls occurs before the Init event of the page. Use this event to read or initialize control properties.

您需要在此事件之前加载您的控件。

而不是 Page.LoadControl("UserControlPath"),您可以控制 ascx 文件的可见性。试试下面的代码。

If m_qrptTOD = "tod" Then
       ascxRptTimeOfDay.Visible = False
End If

请注意,您的 rptTimeOfDay.ascx UserControl 将始终以这种方式加载,您只需更改其可见性。但是,在 Asp.NET 中,如果用户控件不可见,则不会向客户端发送 html 代码。