Click here to Skip to main content
15,905,233 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

Hi Wontbey,

Try this,

Set Old_User_ctrl = nothing
 
Share this answer
 
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 :)
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900