Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7566029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:18:30+00:00 2026-05-30T14:18:30+00:00

I’m adding web user controls to a page dynamically. Using the LoadControl method that

  • 0

I’m adding web user controls to a page dynamically. Using the LoadControl method that only takes a virtual path pointing to the .ascx works pretty nicely. However, the overload of LoadControl that takes a type and an array of parameters is causing me some headaches.

The web user control is instantiated as expected, but the controls contained within the web user control are null and I get an exception as soon as I try to work with them. Strange, because it’s working when use the first version of LoadControl.

The web user control, simple, with a Literal control:

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="MyControl.ascx.vb" Inherits="MyControl" %>
<asp:Literal ID="myLiteral" runat="server"></asp:Literal>

The controls’ code behind:

Public Class MyControl
  Inherits System.Web.UI.UserControl

  Public Property Data As MyData  

  Public Sub New()

  End Sub

  Public Sub New(data As MyData)
    Me.Data = data
  End Sub

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    myLiteral.Text = Data.ID ' The Literal is null, but ONLY when I use the second LoadControl() method!
  End Sub

End Class

And the relevant code from the .aspx from which I’m trying to dynamically load the control:

Private Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
  Dim x = LoadControl(GetType(MyControl), New Object() {New MyData With {.ID = 117}})
  Page.Controls.Add(x)

  ' Using LoadControl("MyControl.ascx") works as expected!
End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:18:32+00:00Added an answer on May 30, 2026 at 2:18 pm

    With a bit of help from this article by Steven Robbins, I ended up with a very convenient extension method instead:

    Imports System.Runtime.CompilerServices
    Imports System.Web.UI
    Imports System.Reflection
    
    Module LoadControls
      <Extension()> _
      Public Function LoadControl(templateControl As TemplateControl, virtualPath As String, ParamArray constructorParams() As Object) As UserControl
        Dim control = TryCast(templateControl.LoadControl(virtualPath), UserControl)
        Dim paramTypes = constructorParams.Select(Function(p) p.GetType()).ToArray
        Dim constructor = control.GetType().BaseType.GetConstructor(paramTypes)
    
        If constructor Is Nothing Then ' Nothing if no such constructor was found.
          Throw New ArgumentException(String.Format("No constructor for control '{0}' with {1} parameter(s) were found.", virtualPath, paramTypes.Count))
        Else
          constructor.Invoke(control, constructorParams)
        End If
    
        Return control
      End Function
    
    End Module
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a aspx page on it i'm dynamically adding the web user controls
I'm dynamically loading user controls adding them to the Controls collection of the web
I have an aspx page that dynamically loads user controls: there is an UpdatePanel
I have inherited a working VB.NET 2.0 web app that has several User Controls
I have a grid view in web user control and I'm dynamically adding the
I have an ASP.NET web form which I am adding a variable number User
I working on adding file uploading to my web application. I'm using an iframe
I have a website that makes use of a web.config. I tried adding in
I'm currently working with a part of my application that uses Dynamic Web User
I've created some Web User Controls (.ascx files) and dropped them into Pages. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.