Object reference error in UserControl's Load event

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default Object reference error in UserControl's Load event

    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
    [email]njsokalski@hotmail.com[/email]
    [url]http://www.nathansokalski.com/[/url]


    Nathan Sokalski Guest

  2. Similar Questions and Discussions

    1. Object Reference Error. Pls Help!
      Hi, I'm having a major issue with one of the custom contols I've created. Basically, it consists of a dropdown, button and some textboxes for the...
    2. object reference error
      Can someone please help. I am running locally XP, IIS5. Am trying forms authentication with no luck in one area. I am using the following script to...
    3. Mysterious Error: Object reference not set to an instance of an object
      Hi There! I'm having a mysterious error right after I login using Forms Authentication in my ASP.NET app. Below is the error... Exception...
    4. HELP! Error Loading ASPX : Object Reference not set to an instance object
      Hello, When i run my aspx i get this weird error: System.NullReferenceException: Object reference not set to an instance of an object. at...
    5. Object reference not set to an instance of an object. .......... Error .... Please Help
      Hi, I am facing a problem, and i don't know why the error is being generated. I am updating a datagrid with an XML file. My code is ...
  3. #2

    Default Re: Object reference error in UserControl's Load event

    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

    --
    [url]http://www.openmymind.net/[/url]
    [url]http://www.codebetter.com/[/url]


    "Nathan Sokalski" <njsokalski@hotmail.com> wrote in message
    news:Oiui$QWMHHA.992@TK2MSFTNGP06.phx.gbl...
    >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
    > [email]njsokalski@hotmail.com[/email]
    > [url]http://www.nathansokalski.com/[/url]
    >
    Karl Seguin Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139