Loading User Control Dynamically

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Loading User Control Dynamically

  1. #1
    Join Date
    Jul 2004

    Loading User Control Dynamically

    Question 1.
    Is it possible to dynamically load a user control in the code-behind file of a client .aspx page or must it be loaded in the .ascx file?

    I tried using the code-behind file but was not successful.

    Question 2.
    Also, is it possible to have more than 1 instance of the same user control on the same page, 1 created during design time and the other loaded dynamically?

    I tried that but only get 1 user control in run time.

    If any of you have the solution to the above, would be a great help if you could kindly take some of your time to type in your solution.

    Thank you in advance.

  2. #2
    Join Date
    Aug 2004
    1) Yes,
    Control c1 = Page.LoadControl("controls/mycontrol.ascx");

    2) Yes it is possible. Not sure how you tried, considering in #1 you said you didn't know how to load it dynamically

    Let me know if you have problems.

  3. #3
    Join Date
    Jul 2004
    In fact I did what you wrote, below is my codes in the code-behind file:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    Dim uc2 As Control
    uc2 = Page.LoadControl("SimpleCalculator.ascx")
    End Sub

    This user control has 3 text boxes and 1 button. I received an error that says (when I dynamically loaded it using code-behind file:

    "Control '_ctl0_Op1' of type 'TextBox' must be placed inside a form tag with runat=server."

    The language I am using is Visual Basic.
    Everything is working fine if I create an instance of the user control by drag-and-drop in design time.

    Thanks for your time and help.

  4. #4
    Join Date
    Jul 2004
    Hi cmm,

    I managed to solve the problem.
    It is because I didn't do the following:
    - specify the classname attribute of the @Control directive in the .ascx file
    - Use the @Reference directive to reference the user control in my web page.

    After doing the above, I was able to create my user control dynamically in the code-behind file.


  5. #5
    Join Date
    May 2005

    Loading user controls dynamically

    Hi, Magdalene:
    Here is an example for your question. It's useful for you and others.

    The user controls in the system are loaded dynamically. so the layout of the page can be changed while the system is running.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center