Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding a User Control to a Custom Control

Status
Not open for further replies.

jby1

Programmer
Apr 29, 2003
403
GB
I am developing a custom control to output an expandable list, with each expandable element being able to contain a different control(s).

I wish to be able to insert a User Control into this.

I have used the LoadControl method to load the control into my page where the custom control is located. I then add the loaded control to the custom control.

When I try to display this loaded control, I get an error "The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)."

Does anybody know what I can do to get around this issue?

Regards
 
Oh, I probably should have mentioned that the User Control doesn't contain any code blocks! I created a very simple User Control which contains a Label, a Textbox and a Button, just for test purposes.
 
Just to make it more interesting, I tried to add a Label control, and still got the same message!
 
I got around that issue, and managed to get the control to render ok.

However, it is still not working as desired, for example the User Control events are not working.

Has anybody managed to successfully dynamically load a User Control (.ascx) into an assembled Custom Control, and get all of the events of the User Control working? Or is this just not possible? Any information on this, however small, would be gratefully received, because I have been looking at this for days and it is slowly driving me mad!
 
If you're dynamically adding a user control, you want to do it on every PostBack early in the page lifecycle and assign the control the exact same id every time.

Are you manually assigning the id before adding the control to the server control's Controls collection? Where do you add the control?
 
Hi

Thanks for you reply.

I have added the Custom Control to the page, and dynamically load the User Controls from the Page Init event like so. ExpandableList is the custom control that I am creating.

<code>
this.ExpandableList1.ContentControls.Add("WebUserControl1.ascx");
this.ExpandableList1.ContentControls.Add("WebUserControl2.ascx");
this.ExpandableList1.ContentControls.Add("WebUserControl3.ascx");
</code>

The custom control then loads the controls into Html table cells with the following code (which resides in a loop), which is called from the overloaded CreateChildControls method in the custom control.

<code> Control ctrl = this.Page.LoadControl(this.ContentControls);
ctrl.ID = this.ID + "_LoadedControl_" + i.ToString();
this.contentCell.Controls.Add(ctrl);
</code>

This gives each loaded control an ID.

The control renders as it should, but I cannot trigger any events on the loaded user controls.

I hope this makes some sense!
 
When in the Page life cycle do the user controls load up? If it's after RaiseChangedEvents or RaisePostBackEvent that could be the source of the problem.
 
PS- Basically that means the controls should load up in the Load event or before.
 
Initially I tried loading them up in Page_Load, and then I tried moving it back to Page Init.

Neither has worked!
 
Did you ensure that each control (including any parent controls like TableCell and child controls) maintain a consistant ID across PostBacks?

To confirm, you can check the ClientID of the controls in the debugger.
 
Yes, the control assigns all the ids of its controls on each postback.
 
Right, but the child controls are appended with a prefix a la INamingContainer so it may be that a parent control's name changes for some reason, thus changing the ClientID of the control because of the new prefix.

Did you compare the ClientIDs (which include the prefix)?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top