Results 1 to 3 of 3

Thread: Dynamically registering a User Control

  1. #1

    Thread Starter
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    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

    If I have been helpful please rate my post. If I haven't tell me!

  2. #2
    Addicted Member
    Join Date
    Aug 2004
    Location
    Cape Town, South Africa
    Posts
    149

    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:
    1. aPlaceholder = CType(aSkin.FindControl("plhContent"), PlaceHolder)
    2. If Not (aPlaceholder Is Nothing) Then
    3.        Dim leftColumn As UserControl
    4.        leftColumn = CType(Me.LoadControl("blah.ascx"), UserControl)
    5.        aPlaceholder.Controls.Add(leftColumn)
    6. End If

    aSkin is just another usercontrol on the form. You can replace it with the actual web form (ie. Me.FindControl("plhContent")).

  3. #3

    Thread Starter
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    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

    If I have been helpful please rate my post. If I haven't tell me!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width