Dynamic control array loading, can't unload control/replace with o



I have a web form with 3 links, depending on which link is clicked a user
control loads, page1.ascx,page2.ascx,page3.ascx

page1.ascx does a search, uses 3 asp:dropdownlists with viewstate true and
auto post back. It loads by default first time.

User clicks on link 2, page2.ascx loads after page1.ascx I need the loaded
control to go away when a different one is clicked, I have seen similar
stuff on threads but I can't understand a clear answer on what syntax and
where in the order?

I do not understand the syntax for controls.remove the sample says
control.remove where control.name =
and there is no controls.name in c sharp syntax for this? Also I am using
control array see below

private void OnPageButtonClick(object sender, System.EventArgs e)
LinkButton pageButton = (LinkButton)sender;
string pageNumber = pageButton.ID.Substring("_pageButton".Length);
ActiveTab = pageNumber;
public void SelectPage(string pageNumber)
Control page = LoadControl(String.Format("Page{0}.ascx", pageNumber));
LinkButton pageButton = FindControl(String.Format("_pageButton{0}",
pageNumber)) as LinkButton;
if (pageButton != null)
pageButton.BackColor = Color.Gold;
pageButton.CssClass = "tabon";
LastActiveControl = page.ID.ToString();





I'm assuming your "_content" is a container Control. If so, try
_content.Controls.Clear(); before you add the next control.



Steven Cheng[MSFT]

Hi Cindy,

I've also noticed your another previous post in the

" 03 dropdownlist selectedindexchanged not firing in usercontrol on"


newsgroup, I've also attached a demo package there. Have you had a chance
to test it? Also, as for the unloading usercontrol, I think we should
follow the following two steps according to my description in the former

1. In the first postback of the 3rd link(which will remove the first 2
usercontrols), we can use Container.Controls.Clear to clear the control
collection, we can also use Controls.Remove to remove the control instance
from it's parent collection( use FindControl to get the reference first)

2. Also, set the flag in ViewState accordingly since the sequential
postback will create dynamic controls denpending on these values.

If have anything unclear or have any problems accessing the demo pages I
attached there, please feel free to post here.


Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no

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