Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > object reference-error with programmatically loading user control

Reply
Thread Tools

object reference-error with programmatically loading user control

 
 
Martine
Guest
Posts: n/a
 
      08-05-2003
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;
}
}
}
 
Reply With Quote
 
 
 
 
Jay Warmack
Guest
Posts: n/a
 
      08-05-2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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;
> }
> }
> }



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User Control to Control Other User Control Jonathan Wood ASP .Net 4 02-02-2010 03:41 PM
"Object reference not set" when loading a textbox in a user control Monty ASP .Net Building Controls 5 03-30-2006 08:48 PM
persisting changes to a control outside a user control from the user control? Mad Scientist Jr ASP .Net 0 03-22-2006 08:02 AM
Loading user control when a link is clicked on another user contro =?Utf-8?B?U3JpZGhhcg==?= ASP .Net 1 11-18-2005 03:00 AM
Dynamically loading user control into Placeholder gives Object reference not set to an instance of an object Phoenix ASP .Net Building Controls 2 06-06-2005 04:02 PM



Advertisments