I\'m trying to return the html representation of a user/server control through a page method. It works when I call the overload which takes the virtual path to the user cont
That overload instantiates the base class, but doesn't instantiate any of the controls on it, so it doesn't work.
I did a quick blog post on a workaround for passing parameters if you're interested.
If you want to have the control completely rendered one way to do this is to instantiate the control as you are now with LoadControl
and then temporarily add it to the control collection of another control or the page itself. This will initialize the life-cycle for that control and cause all of the proper events to be raised. Once you have done this then you can get the rendered HTML or whatever you need from it.
Yes, this is a hack but it will work in a pinch.
On the MSDN page for LoadControl there is this comment at the bottom:
Description
A page that loads a user control using the Page.LoadControl(Type, Object[]) does not seem to create its children added in the ascx file. Using Page.LoadControl(String) works as expected.Comments
Thank you for submitting this issue. We're investigating and will provide an update on status when we have more information.-The Web Platform & Tools Team
Posted by Microsoft on 8/06/2005 at 11:08 AM
This is by-design since the type "TestUC" is actually the base type used by the partial class, it does not contain the proper code to instantiate TextBox1 reference, which is actually defined in the derived type. There are two workarounds: 1. Use LoadControl("TestControl.ascx"), for all practical, this behaves identically to LoadControl(type) but it instantiates the derived type, which knows how to instantiate TextBox1. 2. Use a single file page and adds <%@ Reference %> directive to the page to reference the user control, and assign a classname to the ascx page. Then it's safe to use LoadControl(type)Thanks for reporting the issue.
Web Platform and Tools Team. Posted by Microsoft on 14/06/2005 at 6:31 PM