Question

I have a .ascx file that I can load in no problem using

<uc1:EL ID="EL1" BusinessID="8" runat="server" />

Where BusinessID is a Public Property.

Public Property BusinessID As Integer
    Set(ByVal value As Integer)
        _BusinessID = value
    End Set
    Get
        Return _BusinessID
    End Get
End Property

I may need to load this ascx file several times into a placeholder with different BusinessID variable values.

What is the equivalent LoadControl way of doing so?

Was it helpful?

Solution

First, you need to create an instance of the user control, then get a handle on the place holder. Then you can add one to the other. Example:

 'get place holder
 Dim getPh As New PlaceHolder
 getPh = CType(Me.FindControl("myPlaceHolder"), PlaceHolder)

 'get user controls    
 Dim newUserControl As New user_controls_myControlName
 newUserControl = CType(LoadControl("~/user_controls/myControlName.ascx"), user_controls_myControlName)    

 getPh.Controls.Add(newPortlet)

Once you create an instance of the user control, you have access to all its properties, including BusinessID and you can assign it whatever you want.

Forgot one thing, you will need to add a reference in the client side code of your ascx file, like this:

<%@ Reference Control="~/user_controls/myControlName.ascx"%>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top