Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically creating user controls 1

Status
Not open for further replies.

bernie321

Programmer
Jan 7, 2004
477
GB
Hi

I am creating user controls for cross browser compatibility.

The ASPX page references a user control which in its page_load sub will generate and return another user control based on which browser/version.

I have been attempting to develop this code in the page_load but have been unsuccesful.

Does anyone have any VB .NET ideas?

Thanks B
 
I am now using this code:

Dim path As String = "topbar_all.ascx"
Dim ctl As UserControl = CType(Page.LoadControl(path), UserControl)
Page.Controls.Add(ctl)

But i get the error: "The Controls collection cannot be
modified because the control contains code blocks (i.e. <% ... %>). "

The code does not contain any code blocks.

Thanks B
 
Hey Bernie -

Instead of adding it to the Page's controls collection, use a PlaceHolder on the page. The add your dynamicly loaded user control to the placeholder's controls collection.

Code:
<asp:placeholder runat="server" id="controlPlace" />

Code:
Dim path As String = "topbar_all.ascx"
Dim ctl As UserControl = CType(Page.LoadControl(path), UserControl)
Me.controlPlace.Controls.Add(ctl)

Greetings,
Dragonwell
 
Hi Dragonwell,

I got this code working just before you posted:

usrCtrl = LoadControl("side_ie3_opera35.ascx")
Me.Controls.Add(usrCtrl)

Which works fine but now I have found another problem. I am using 2 user controls:

number1.aspx
number2.ascx
number3.ascx

In number1.aspx i am using this code to pass a parameter to number2.ascx:

<CodeLib:usrSideMenu gstrURL="contact.aspx" runat="server" ID="Usrsidemenu1" />

and used this code in number2.ascx to retrieve the value:

Public _gstrURL As String
Public Property gstrURL() As String
Get
Return _gstrURL
End Get
Set(ByVal Value As String)
_gstrURL = Value
End Set
End Property

The problem is that I need to retrieve the value _gstrURL in number3.ascx.

Can i pass it in the code:
usrCtrl = LoadControl("side_ie3_opera35.ascx")
Me.Controls.Add(usrCtrl)

Thanks B
 
In the code for number3, you can access it's parent control. Since you know it's a number2, you can cast it to number2. Once it's cast, you have access to it's public properties as a number2.


Code:
Dim parentControl as number2 = DirectCast(Me.Parent, number2)

Dim stringFromNumber2 = number2._gstrURL



Greetings,
Dragonwell
 
Thank you Dragonwell - It works a treat!!

Thanks again B
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top