This is a migrated thread and some comments may be shown as answers.

[Solved] Dynamic loading of UserControls

1 Answer 193 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Martin
Top achievements
Rank 1
Martin asked on 29 May 2008, 09:43 AM
Hi

I have a question about dynamic loading of controls...
I've got my application working fine using your example
at www.telerik.com/help/aspnet/ajax/ajxloadusercontrols.html

On loading, all my dynamic controls run initialisation code
which typically loads data into the controls UI from a dataset.

Using the example code below:

After the initial page is loaded, I then click on Button2
and the sequence of events is ....

1) ControlONE.ascx is first re-loaded in the "Page Load event".
    - LoadUserControl(LatestLoadedControlName)

2) ControlTWO.ascx is then loaded inside the navigation button "event" and ControlONE.ascx is removed.
    - LoadUserControl("ControlTWO.ascx")

It seems wasteful to re-create the previous control and run initialise code again...

Is this just the way it has to be done ?
I assume because this is happening on the server side then the re-loading of the original
control has no performance effect on the client side?

Regards

Martin Hoey


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not (LatestLoadedControlName Is Nothing) Then
            LoadUserControl(LatestLoadedControlName)
        Else
            LoadUserControl("ControlONE.ascx")
        End If
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        LoadUserControl("ControlONE.ascx")
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        LoadUserControl("ControlTWO.ascx")
    End Sub

    Public Sub LoadUserControl(ByVal controlName As String)
        If Not (LatestLoadedControlName Is Nothing) Then
            Dim previousControl As Control = Panel1.FindControl(LatestLoadedControlName.Split("."c)(0))
            If Not (previousControl Is Nothing) Then
                Me.Panel1.Controls.Remove(previousControl)
            End If
        End If
        Dim userControlID As String = controlName.Split("."c)(0)
        Dim targetControl As Control = Panel1.FindControl(userControlID)
        If targetControl Is Nothing Then
            Dim userControl As UserControl = CType(Me.LoadControl(controlName), UserControl)
            'slashes and tildes are forbidden
            userControl.ID = userControlID.Replace("/", "").Replace("~", "")
            Me.Panel1.Controls.Add(userControl)
            LatestLoadedControlName = controlName
        End If
    End Sub 'LoadUserControl

    Private Property LatestLoadedControlName() As String
        Get
            Return CStr(ViewState("LatestLoadedControlName"))
        End Get
        Set(ByVal value As String)
            ViewState("LatestLoadedControlName") = value
        End Set
    End Property

1 Answer, 1 is accepted

Sort by
0
Martin
Top achievements
Rank 1
answered on 29 May 2008, 10:18 AM
Hi

I think I understand now....

If I remove :
LoadUserControl(LatestLoadedControlName)
then I eventually get an error "Failed to load viewstate"

Any further clarification appreciated but in the meantime I'm happy (if confused!)
to close this issue...

Regards

Martin

Tags
Ajax
Asked by
Martin
Top achievements
Rank 1
Answers by
Martin
Top achievements
Rank 1
Share this question
or