N
Nick Stansbury
Hi,
I've observed some weirdness with the ControlsCollection - do people
think this behaviour makes sense?
I populate a list, comprised of list-items which are user controls at
run time - because they are not tracked by the viewstate I manually "cache"
the controls collection of the list form to session memory at PreRender
thus:
viewstate("Controls") = New Guid().NewGuid().ToString
Session(viewstate("Controls")) = pnlResults.Controls
Now on page_load - i get them back with this:
Dim ControlCollectionID As String
ControlCollectionID = viewstate("Controls")
If Not Session(ControlCollectionID) Is Nothing Then
Dim controls As ControlCollection
controls = Session(ControlCollectionID)
For Each currentCtl As Control In controls
If TypeOf (currentCtl) Is UserControl Then
'write a routine to load the user control in here - first make a
duplicate copy
Dim Cuser As UserControl
Cuser = currentCtl
pnlResults.Controls.Add(Cuser)
Else
'if it isn't a user control then just add it in here as normal
Me.pnlResults.Controls.Add(currentCtl)
End If
Now when I do this you'd expect the CurrentCtl.Controls reference to now
point to the panel's controls collection *not* to the controls collection in
session memory. This is does. However it *also* removes the reference from
the session controls collection to CurrentCtl - the result - the for each
fails to add the last control in the collection (the count is reduced by one
as the reference to CurrentCtl is removed). Does this seem more than a
little bit weird to anyone else? Can anyone explain why it should do this?
Thanks
Nick
I've observed some weirdness with the ControlsCollection - do people
think this behaviour makes sense?
I populate a list, comprised of list-items which are user controls at
run time - because they are not tracked by the viewstate I manually "cache"
the controls collection of the list form to session memory at PreRender
thus:
viewstate("Controls") = New Guid().NewGuid().ToString
Session(viewstate("Controls")) = pnlResults.Controls
Now on page_load - i get them back with this:
Dim ControlCollectionID As String
ControlCollectionID = viewstate("Controls")
If Not Session(ControlCollectionID) Is Nothing Then
Dim controls As ControlCollection
controls = Session(ControlCollectionID)
For Each currentCtl As Control In controls
If TypeOf (currentCtl) Is UserControl Then
'write a routine to load the user control in here - first make a
duplicate copy
Dim Cuser As UserControl
Cuser = currentCtl
pnlResults.Controls.Add(Cuser)
Else
'if it isn't a user control then just add it in here as normal
Me.pnlResults.Controls.Add(currentCtl)
End If
Now when I do this you'd expect the CurrentCtl.Controls reference to now
point to the panel's controls collection *not* to the controls collection in
session memory. This is does. However it *also* removes the reference from
the session controls collection to CurrentCtl - the result - the for each
fails to add the last control in the collection (the count is reduced by one
as the reference to CurrentCtl is removed). Does this seem more than a
little bit weird to anyone else? Can anyone explain why it should do this?
Thanks
Nick