object reference-error with programmatically loading user control

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

  1. #1

    Default object reference-error with programmatically loading user control

    Hi there!

    I have a problem with programmatically adding user controls to my
    mobile webforms. If I load my usercontrol programmatically (in the
    Page_Load), the object is instantiated, I have access to the methods
    and properties from the Page_Load, no problem there. But as soon as I
    want access to the user control from another procedure on the same
    page, I get the next error message:

    "Object reference not set to an instance of an object."

    When I declaratively add the user control to my page, everything's ok,
    I have access to the user control from the other procedure. In this
    particular case, declaratively adding the user control is ok, but I
    would really like to know what I am doing wrong in the first case.

    Code is below. The last line of code, in Command_Login_Click is
    causing the error. The exact same line works fine in Page_Load. Also,
    it works fine in the Page_Load and Command_Login_Click when the
    control is declaratively added.

    Any help would be much appreciated!
    seeyoubye, Martine.
    ---------------------------
    CODE IN LOGIN.ASPX.CS:

    protected tank.user newUser;

    private void Page_Load(object sender, System.EventArgs e)
    {
    Control newUser = LoadControl("user.ascx");
    Page.Controls.Add(newUser);
    }

    public void Command_Login_Click(object sender, System.EventArgs e)
    {
    testlabel.Text = ((user)newUser).Function.ToString();
    }

    CODE IN LOGIN.ASPX:

    <%@ Reference Control="user.ascx" %>

    CODE IN USER.ASCX.CS:

    public abstract class user : System.Web.UI.UserControl
    {
    public String Function
    {
    get
    {
    return "test";
    }
    set
    {
    Session["User_Function"] = value;
    }
    }
    }
    Martine Guest

  2. Similar Questions and Discussions

    1. "Object reference not set" when loading a textbox in a user control
      Hi All, I have a user control that shows name and address for a person in a text box. When the page hosting the control loads up, it calls a...
    2. Programmatically Get to User Control Properties
      I created a User Control, called in an Asp.Net page. First, I've registered the control at the top of the page: <%@ Register TagPrefix="uc1"...
    3. Dynamically loading user control into Placeholder gives Object reference not set to an instance of an object
      I've created user controls that contain listboxes that are dynamically populated from the database. In the html view of the user control...
    4. Custom Control Problem :: Object reference not set to an instance of an object
      Hi All! I have the following Custom Control file... '########### WebUserControl1.ascx ############# <%@ Control Language="vb"...
    5. 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...
  3. #2

    Default Re: object reference-error with programmatically loading user control

    Martine,

    Something seems wrong here:

    protected tank.user newUser;

    private void Page_Load(object sender, System.EventArgs e)
    {
    Control newUser = LoadControl("user.ascx");
    Page.Controls.Add(newUser);
    }

    You declare newUser as a protected object of type tank.user, this seems
    right. But then in the Page_Load you declare newUser as an object of type
    Control where you then assign it a reference to the object returned from
    LoadControl. In this case the newUser object is now just a local reference
    variable in the Page_Load method. So your reference in the Login method
    will have a null object reference because you never assigned an object to
    the protected reference variable. Make sense?

    So what I would do is change your code to this:

    protected tank.user newUser;

    private void Page_Load(object sender, System.EventArgs e)
    {
    newUser = LoadControl("user.ascx");
    Page.Controls.Add(newUser);
    }

    See if that works,

    Jay Warmack, MCAD
    IT Consultant


    "Martine" <mvaniperen@poolmanager.nl> wrote in message
    news:b7e235c8.0308050635.41db09f3@posting.google.c om...
    > Hi there!
    >
    > I have a problem with programmatically adding user controls to my
    > mobile webforms. If I load my usercontrol programmatically (in the
    > Page_Load), the object is instantiated, I have access to the methods
    > and properties from the Page_Load, no problem there. But as soon as I
    > want access to the user control from another procedure on the same
    > page, I get the next error message:
    >
    > "Object reference not set to an instance of an object."
    >
    > When I declaratively add the user control to my page, everything's ok,
    > I have access to the user control from the other procedure. In this
    > particular case, declaratively adding the user control is ok, but I
    > would really like to know what I am doing wrong in the first case.
    >
    > Code is below. The last line of code, in Command_Login_Click is
    > causing the error. The exact same line works fine in Page_Load. Also,
    > it works fine in the Page_Load and Command_Login_Click when the
    > control is declaratively added.
    >
    > Any help would be much appreciated!
    > seeyoubye, Martine.
    > ---------------------------
    > CODE IN LOGIN.ASPX.CS:
    >
    > protected tank.user newUser;
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > Control newUser = LoadControl("user.ascx");
    > Page.Controls.Add(newUser);
    > }
    >
    > public void Command_Login_Click(object sender, System.EventArgs e)
    > {
    > testlabel.Text = ((user)newUser).Function.ToString();
    > }
    >
    > CODE IN LOGIN.ASPX:
    >
    > <%@ Reference Control="user.ascx" %>
    >
    > CODE IN USER.ASCX.CS:
    >
    > public abstract class user : System.Web.UI.UserControl
    > {
    > public String Function
    > {
    > get
    > {
    > return "test";
    > }
    > set
    > {
    > Session["User_Function"] = value;
    > }
    > }
    > }

    Jay Warmack 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