Reputation: 6085
I dynamically create a User Control from XML via XSLT. Output is a string with a content like this:
<%@ Control Language="C#" AutoEventWireup="true" Inherits="Library.Web.UI.GeneratedFormBase, MyAssembly" %>
<div class="myCssClass">
<asp:TextBox ID="d" runat="server" OnTextChanged="OnTextChanged" />
<asp:Label runat="server" AssociatedControlID="SomeName" AccessKey="n">Label Text</asp:Label>
<asp:TextBox ID="SomeName" runat="server" OnTextChanged="OnTextChanged" />
<asp:Label runat="server" AssociatedControlID="SomeOtherName">Welcome</asp:Label>
<asp:TextBox ID="SomeOtherName" runat="server" OnTextChanged="OnTextChanged" />
<asp:Button ID="OK" runat="server" OnClick="ButtonClick" Text="Save" />
</div>
I now use Page.ParseControl(theGeneratedString) to create this control dynamically.
The type that is declared in Inherits
is existing and can be found. If I declare another (i.e. non-existing) type there, a Parser Error
Exception is thrown, so I am totally convinced that the parser looks for this type and finds it.
Nevertheless, the Control that is generated from the ParseControl
is of type System.Web.UI.Control and not of the control that is stated (and obviously also parsed and located) in the Inherits-declaration.
Why is that and how can I ensure that the control is of the correct type?
Upvotes: 1
Views: 400
Reputation: 6085
Okay, after using a bit reflector it seems obvious why the Control is of the 'wrong' class. So ParseControl is simply the wrong method to do this. The correct one is LoadControl, but to use it I need to provide the generated form via a VirtualPathProvider. So it's a lot more work to get the control to parse correctly, but when using this approach the control is loaded, parsed, compiled and derived from the correct type.
Upvotes: 1