object reference-error with programmatically loading user control

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martine

    #1

    object reference-error with programmatically loading user control

    Hi there!

    I have a problem with programmaticall y adding user controls to my
    mobile webforms. If I load my usercontrol programmaticall y (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_C lick is
    causing the error. The exact same line works fine in Page_Load. Also,
    it works fine in the Page_Load and Command_Login_C lick 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(objec t sender, System.EventArg s e)
    {
    Control newUser = LoadControl("us er.ascx");
    Page.Controls.A dd(newUser);
    }

    public void Command_Login_C lick(object sender, System.EventArg s e)
    {
    testlabel.Text = ((user)newUser) .Function.ToStr ing();
    }

    CODE IN LOGIN.ASPX:

    <%@ Reference Control="user.a scx" %>

    CODE IN USER.ASCX.CS:

    public abstract class user : System.Web.UI.U serControl
    {
    public String Function
    {
    get
    {
    return "test";
    }
    set
    {
    Session["User_Funct ion"] = value;
    }
    }
    }
  • Jay Warmack

    #2
    Re: object reference-error with programmaticall y loading user control

    Martine,

    Something seems wrong here:

    protected tank.user newUser;

    private void Page_Load(objec t sender, System.EventArg s e)
    {
    Control newUser = LoadControl("us er.ascx");
    Page.Controls.A dd(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(objec t sender, System.EventArg s e)
    {
    newUser = LoadControl("us er.ascx");
    Page.Controls.A dd(newUser);
    }

    See if that works,

    Jay Warmack, MCAD
    IT Consultant


    "Martine" <mvaniperen@poo lmanager.nl> wrote in message
    news:b7e235c8.0 308050635.41db0 9f3@posting.goo gle.com...[color=blue]
    > Hi there!
    >
    > I have a problem with programmaticall y adding user controls to my
    > mobile webforms. If I load my usercontrol programmaticall y (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_C lick is
    > causing the error. The exact same line works fine in Page_Load. Also,
    > it works fine in the Page_Load and Command_Login_C lick 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(objec t sender, System.EventArg s e)
    > {
    > Control newUser = LoadControl("us er.ascx");
    > Page.Controls.A dd(newUser);
    > }
    >
    > public void Command_Login_C lick(object sender, System.EventArg s e)
    > {
    > testlabel.Text = ((user)newUser) .Function.ToStr ing();
    > }
    >
    > CODE IN LOGIN.ASPX:
    >
    > <%@ Reference Control="user.a scx" %>
    >
    > CODE IN USER.ASCX.CS:
    >
    > public abstract class user : System.Web.UI.U serControl
    > {
    > public String Function
    > {
    > get
    > {
    > return "test";
    > }
    > set
    > {
    > Session["User_Funct ion"] = value;
    > }
    > }
    > }[/color]


    Comment

    Working...