Loading UserControl from DLL

  • Thread starter Thread starter Omikron
  • Start date Start date
O

Omikron

Hi,
I have a problem with loading user controls dynamically on the page. I
have two simple projects: first includes user control with one
textbox.

Public Partial Class test
Inherits System.Web.UI.UserControl

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.PreRender
TextBox1.Text = "I am loaded now " & Now.ToString("dd MMM yyyy
hh:mm:ss")
End Sub
End Class

In the second project I am putting a reference to the first one and
want to create an instance of the user control. In the Page_Load it
looks like this:

Dim str_Assembly As String = "WebApplication1, Version=1.0.0.0,
Culture=neutral, PublicToken=null"
Dim asm_ControlAssembly As System.Reflection.Assembly =
System.Reflection.Assembly.Load(str_Assembly)

Dim obj_Control As Control = CType(asm_ControlAssembly.CreateInstance
("WebApplication1.test"), Control)
PlaceHolder1.Controls.Add(obj_Control)

.....
Everything is fine, control is created, added, except the fact that
TextBox1 from the UserControl is not initalized and so I'm getting
null reference exception when it comes to Page_Load call in
UserControl...

Thanks in advance
Bartek
 
In general, I do not suggest using user controls in this manner, precisely
due to the problems you are having. There are ways around this, but once you
go through the motions, you are simply ending up with a kludged server
control in most instances. If so, you are better just writing a server
control in the first place.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Blog:
http://feeds.feedburner.com/GregoryBeamer

*************************************************
| Think outside the box! |
*************************************************
 
Back
Top