Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi there, I'm hoping one of you guys can help me out here.
 
I'm using vb.net
 
I have a web page that holds a user control on it. Quite a few functions/subs etc. reference it by it's ID (as you would expect)
 
Once the user control has done all it needs to, It would be much, much easier for me to remove the object and reload it, but the new control would need to use the same ID.
 
So I've tried this :
Dim Old_User_ctrl As Control
Old_User_ctrl= Me.FindControl("MyControl1")
divPlaceholder.Controls.Remove(Old_User_ctrl)
 
Dim NEW_User_ctrl As New Control
NEW_User_ctrl = LoadControl("~\Controls\MyControl.ascx")
NEW_User_ctrl.ID = "MyControl1"
divPlaceholder.Controls.Add(NEW_User_ctrl)
 
However; once I continue running through my code, I've found that I get an error :
 
Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Multiple controls with the same ID 'MyControl1' were found. FindControl requires that controls have unique IDs.
 
Can anyone tell me how to properly dispose of the user control object ?
 
Thanks folks,
Wotney
Posted 7-Dec-10 10:47am
Wotney133
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi Wontbey,
 
Try this,
 
Set Old_User_ctrl = nothing
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Unfortunately not.
Once I run
Dim Old_User_ctrl As Control
Old_User_ctrl= Me.FindControl("MyControl1")
divPlaceholder.Controls.Remove(Old_User_ctrl)
 
Old_User_ctrl = Nothing
 
I break the code and using the immediate window I can still get to the control eg.
 
?MyControl1.ID
"MyControl1"
 
and later in the code I get the same error as before...
 
Multiple controls with the same ID 'MyControl1' were found.
 
But - I have worked around my problem now using a different technique. I have basically reset all of the existing control's variables, child controls etc. So I can use it like it was a fresh control.
 
So, I've solved my immediate problem, but if anyone can answer my original question - that'd be great for future reference !
 
Thanks Smile | :)
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 8 Dec 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100