How to unload a usercontrol

L

Lars Pedersen

My app is adding a usercontrol at runtime. How is it possible to remove this
usercontrol? Have tried Page.Controls.Remove(UserControl);, but that wont do
the trick - any suggestions?

-Lars
 
N

Nicholas Paldino [.NET/C# MVP]

Lars,

At what point are you trying to remove the control? Once you have
written to the response stream, you really can't take it back (unless you
are buffering). Are you doing anything that might commit the control to the
stream before you are rendering it?
 
L

Lars Pedersen

Nicholas,

I'm just using a placeholder as a container for the usercontrol. In a
buttonclick event I'm adding a usercontrol to the placeholder, and at some
point, another control will be added to the placeholder, and then the first
usercontrol should be removed from the placeholder.

Is there maybe a way to make it invisible?
-Lars

Nicholas Paldino said:
Lars,

At what point are you trying to remove the control? Once you have
written to the response stream, you really can't take it back (unless you
are buffering). Are you doing anything that might commit the control to the
stream before you are rendering it?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Lars Pedersen said:
My app is adding a usercontrol at runtime. How is it possible to remove this
usercontrol? Have tried Page.Controls.Remove(UserControl);, but that
wont
do
the trick - any suggestions?

-Lars
 
Joined
Sep 15, 2006
Messages
10
Reaction score
0
I found you have to do it like this:

string controlPath = "~/_controltemplates/YourApp/somecontrol.ascx";
Control c = LoadControl(controlPath);
ph.Controls.Clear();
ph.Controls.Remove(c);
c.Dispose();

UserControl uc = LoadControl(controlPath);
uc.ID = "ucMyControl";
ph.Control.Add(uc);

But if you're trying to replace that control with another, you have to record what your last control was somehow, so you can know which controlPath to put in there. People have been putting it in ViewState and calling this (the code above) in a function on Page_Load, but if you can't do it that way (like SharePoint won't let you, you have to use Page_Init, and there are several other reasons/factors for having to do it there), ViewState hasn't been initialized yet in Page_Init. Neither have hidden fields. So I haven't figured out how to tell what control it's supposed to remove, and it returns an error if you choose poorly or all of them. I finally got reading from a cookie to work, but you have to read from it and write back to it in Page_Init, and write to it during the operation that switches the controls, so the cookie always has the right one in it. So you would follow that code above up with:

HttpCookie myCookie = new HttpCookie("myCookieFile");
myCookie.Value = controlPath;
Response.Cookies.add(myCookie);

You read the cookie and reload the control using the same procedure as that first paragraph, setting controlPath = myCookie.Value.

@ making controls "invisible": It's not practical to make my controls "invisible" (setting Visible to "False") because I don't want it to take the time to load all of the controls I'm adding - that's the whole point, I'm trying to save cycles on the page and load only the user control the user selects from a dropdown. I would think that would be the case with most people that are dynamically adding/removing user controls on the page, too.
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top