[RESOLVED] asp.net 2.0 user control problem
I have a default page called default.aspx and a web user control called test.ascx in a folder called controls. Now, if I drag the control on to default.aspx from visual studio and set the properties, it works fine. The problem comes when I try to add the control to default.aspx from the codebehind default.aspx.vb
VB Code:
'test.ascx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = Me.ID
End Sub
VB Code:
'default.aspx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim MyControl as New Controls_Test
MyControl.ID = "SomeID"
End Sub
when I run this, I get a "NullReferenceException" error on the line Label1.Text = Me.ID in the test.ascx.vb file. It is telling me that the Object Label1 doesn't exist. I am really confussed with 2.0 because you don't dimesion the controls in the codebehind.
Re: asp.net 2.0 user control problem
Ok, I googled some more and found that you have to do this:
VB Code:
Dim ctl As Control = Me.LoadControl("Controls/Test.ascx")
Me.AddParsedSubObject(ctl)
Dim MyControl As Controls_Test = CType(ctl, Controls_Test)
I think it has to do with the control not being part of the assembly when the page is run (I am not sure if it gets compiled into its own DLL) I also read that if you dynamically add a control like this, you have to have the <@ Register on the aspx page. This link helped me http://west-wind.com/weblog/posts/3016.aspx