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
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