Results 1 to 2 of 2

Thread: [RESOLVED] asp.net 2.0 user control problem

  1. #1

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141

    Resolved [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:
    1. 'test.ascx.vb
    2. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    3.         Label1.Text = Me.ID
    4. End Sub
    VB Code:
    1. 'default.aspx.vb
    2. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    3.    Dim MyControl as New Controls_Test
    4.    MyControl.ID = "SomeID"
    5. 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.
    Last edited by blindlizard; Nov 14th, 2005 at 03:36 PM.
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

  2. #2

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141

    Re: asp.net 2.0 user control problem

    Ok, I googled some more and found that you have to do this:
    VB Code:
    1. Dim ctl As Control = Me.LoadControl("Controls/Test.ascx")
    2.         Me.AddParsedSubObject(ctl)
    3.         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
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width