Object reference error in UserControl's Load event

Discussion in 'ASP .Net Web Controls' started by Nathan Sokalski, Jan 6, 2007.

  1. I have a UserControl that I declare programmatically as follows:

    Dim userctrl as New rightside_portal()

    The codebehind file for this UserControl looks like the following:

    Partial Public Class rightside_portal : Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Handles Me.Load

    Const renewurl As String = "http://www.renewingsite.com/"

    Me.lblExpiresOn.Text = String.Format("IMPORTANT<br/>Your account is set to
    expire {0}. To renew your membership, please ", "EXPIRATIONDATE")

    Me.lnkRenew.NavigateUrl = renewurl

    Me.lnkSaveToday.NavigateUrl = renewurl

    End Sub

    End Class


    However, I receive the following error when I run the application:

    Server Error in '/' Application.
    --------------------------------------------------------------------------------

    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the
    current web request. Please review the stack trace for more information
    about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set
    to an instance of an object.

    Source Error:

    An unhandled exception was generated during the execution of the
    current web request. Information regarding the origin and location of the
    exception can be identified using the exception stack trace below.

    Stack Trace:

    [NullReferenceException: Object reference not set to an instance of an
    object.]
    AFBE.rightside_portal.Page_Load(Object sender, EventArgs e) +59
    System.Web.UI.Control.OnLoad(EventArgs e) +99
    System.Web.UI.Control.LoadRecursive() +47
    System.Web.UI.Control.LoadRecursive() +131
    System.Web.UI.Control.LoadRecursive() +131
    System.Web.UI.Control.LoadRecursive() +131
    System.Web.UI.Control.LoadRecursive() +131
    System.Web.UI.Page.ProcessRequestMain(Boolean
    includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061



    --------------------------------------------------------------------------------
    Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET
    Version:2.0.50727.210

    I am pretty sure the problem has something to do with the fact that I am
    declaring the UserControl in my codebehind rather than my *.aspx file, but I
    am not quite sure what I need to do differently. If anyone can help me, I
    would appreciate it. Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
     
    Nathan Sokalski, Jan 6, 2007
    #1
    1. Advertising

  2. Nathan Sokalski

    TiSch Guest

    On 6 Jan., 08:28, "Nathan Sokalski" <> wrote:
    > I have a UserControl that I declare programmatically as follows:
    >
    > Dim userctrl as New rightside_portal()
    >
    > The codebehind file for this UserControl looks like the following:
    >
    > Partial Public Class rightside_portal : Inherits System.Web.UI.UserControl
    >
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    > Handles Me.Load
    >
    > Const renewurl As String = "http://www.renewingsite.com/"
    >
    > Me.lblExpiresOn.Text = String.Format("IMPORTANT<br/>Your account is set to
    > expire {0}. To renew your membership, please ", "EXPIRATIONDATE")
    >
    > Me.lnkRenew.NavigateUrl = renewurl
    >
    > Me.lnkSaveToday.NavigateUrl = renewurl
    >
    > End Sub
    >
    > End Class
    >
    > However, I receive the following error when I run the application:
    >
    > Server Error in '/' Application.
    > --------------------------------------------------------------------------------
    >
    > Object reference not set to an instance of an object.
    > Description: An unhandled exception occurred during the execution of the
    > current web request. Please review the stack trace for more information
    > about the error and where it originated in the code.
    >
    > Exception Details: System.NullReferenceException: Object reference not set
    > to an instance of an object.
    >
    > Source Error:
    >
    > An unhandled exception was generated during the execution of the
    > current web request. Information regarding the origin and location of the
    > exception can be identified using the exception stack trace below.
    >
    > Stack Trace:
    >
    > [NullReferenceException: Object reference not set to an instance of an
    > object.]
    > AFBE.rightside_portal.Page_Load(Object sender, EventArgs e) +59
    > System.Web.UI.Control.OnLoad(EventArgs e) +99
    > System.Web.UI.Control.LoadRecursive() +47
    > System.Web.UI.Control.LoadRecursive() +131
    > System.Web.UI.Control.LoadRecursive() +131
    > System.Web.UI.Control.LoadRecursive() +131
    > System.Web.UI.Control.LoadRecursive() +131
    > System.Web.UI.Page.ProcessRequestMain(Boolean
    > includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061
    >
    > --------------------------------------------------------------------------------
    > Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET
    > Version:2.0.50727.210
    >
    > I am pretty sure the problem has something to do with the fact that I am
    > declaring the UserControl in my codebehind rather than my *.aspx file, but I
    > am not quite sure what I need to do differently. If anyone can help me, I
    > would appreciate it. Thanks.
    > --
    > Nathan Sokalski
    > ://www.nathansokalski.com/


    Hello,
    you have to load a WebUsercontrol in the Codebehind:
    dim myControl as rightside_portal
    =DirectCast(Page.LoadControl(UrlToAscx),rightside_portal)

    regards,
    tim
     
    TiSch, Jan 6, 2007
    #2
    1. Advertising

  3. Nathan Sokalski

    Karl Seguin Guest

    you can't create a "new UserContorl();"

    you need to use Page.LoadControl("~/controls/test.ascx");

    Using "new" creates a new instance of the class, but doesn't instanticate
    the aspx class. therefore lblExpires is never created and you end up with a
    null reference.

    Karl

    --
    http://www.openmymind.net/
    http://www.codebetter.com/


    "Nathan Sokalski" <> wrote in message
    news:Oiui$...
    >I have a UserControl that I declare programmatically as follows:
    >
    > Dim userctrl as New rightside_portal()
    >
    > The codebehind file for this UserControl looks like the following:
    >
    > Partial Public Class rightside_portal : Inherits System.Web.UI.UserControl
    >
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles Me.Load
    >
    > Const renewurl As String = "http://www.renewingsite.com/"
    >
    > Me.lblExpiresOn.Text = String.Format("IMPORTANT<br/>Your account is set to
    > expire {0}. To renew your membership, please ", "EXPIRATIONDATE")
    >
    > Me.lnkRenew.NavigateUrl = renewurl
    >
    > Me.lnkSaveToday.NavigateUrl = renewurl
    >
    > End Sub
    >
    > End Class
    >
    >
    > However, I receive the following error when I run the application:
    >
    > Server Error in '/' Application.
    > --------------------------------------------------------------------------------
    >
    > Object reference not set to an instance of an object.
    > Description: An unhandled exception occurred during the execution of the
    > current web request. Please review the stack trace for more information
    > about the error and where it originated in the code.
    >
    > Exception Details: System.NullReferenceException: Object reference not set
    > to an instance of an object.
    >
    > Source Error:
    >
    > An unhandled exception was generated during the execution of the
    > current web request. Information regarding the origin and location of the
    > exception can be identified using the exception stack trace below.
    >
    > Stack Trace:
    >
    > [NullReferenceException: Object reference not set to an instance of an
    > object.]
    > AFBE.rightside_portal.Page_Load(Object sender, EventArgs e) +59
    > System.Web.UI.Control.OnLoad(EventArgs e) +99
    > System.Web.UI.Control.LoadRecursive() +47
    > System.Web.UI.Control.LoadRecursive() +131
    > System.Web.UI.Control.LoadRecursive() +131
    > System.Web.UI.Control.LoadRecursive() +131
    > System.Web.UI.Control.LoadRecursive() +131
    > System.Web.UI.Page.ProcessRequestMain(Boolean
    > includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061
    >
    >
    >
    > --------------------------------------------------------------------------------
    > Version Information: Microsoft .NET Framework Version:2.0.50727.42;
    > ASP.NET Version:2.0.50727.210
    >
    > I am pretty sure the problem has something to do with the fact that I am
    > declaring the UserControl in my codebehind rather than my *.aspx file, but
    > I am not quite sure what I need to do differently. If anyone can help me,
    > I would appreciate it. Thanks.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
     
    Karl Seguin, Jan 6, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. huobazi
    Replies:
    1
    Views:
    698
    David Waz...
    Jul 3, 2003
  2. Nathan Sokalski
    Replies:
    1
    Views:
    377
    Karl Seguin
    Jan 6, 2007
  3. Replies:
    3
    Views:
    3,719
    Arthur
    Jun 23, 2008
  4. Event not firing in usercontrol inside usercontrol

    , Dec 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    185
  5. Nathan Sokalski

    Object reference error in UserControl's Load event

    Nathan Sokalski, Jan 6, 2007, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    153
    Karl Seguin
    Jan 6, 2007
Loading...

Share This Page