Dynamically registering a User Control
I've managed to load a User Control dynamically using (where hldContent is a PlaceHolder control):
Code:
IntroText_ascx intro = (IntroText_ascx) LoadControl("IntroText.ascx");
intro.ID = "intro";
intro.ContentText = "Example";
hldContent.Controls.Add(intro);
However I want to be able to add the register statement needed at runtime not in the source code.
Code:
<%@ Register TagPrefix="HROC" TagName="IntroText" Src="IntroText.ascx" %>
Anyone got any idea if this is possible?
Basically I want to load user controls from a database as and when they are needed and not need to have 100's of register statements on every page.
DJ
Re: Dynamically registering a User Control
From my experience you do not need to add the register prefix tag in the html if you are loading the controls into placeholders dynamically...
What error is it giving you if you don't add the register prefix tag?
Here is some of my code (vb)
VB Code:
aPlaceholder = CType(aSkin.FindControl("plhContent"), PlaceHolder)
If Not (aPlaceholder Is Nothing) Then
Dim leftColumn As UserControl
leftColumn = CType(Me.LoadControl("blah.ascx"), UserControl)
aPlaceholder.Controls.Add(leftColumn)
End If
aSkin is just another usercontrol on the form. You can replace it with the actual web form (ie. Me.FindControl("plhContent")).
Re: Dynamically registering a User Control
I get the following error if I do not at the register statement:
The type or namespace name 'IntroText_ascx' could not be found (are you missing a using directive or an assembly reference?).
As soon as it is added it works fine.
DJ