473,385 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

error in adding user controls at runtime

Hi All,

I have created my own WebControl and want to add it in my aspx page at
runtime.

it's compiling perfectly, but when i m going to execute, it gives me
error of "Object reference not set to an instance of an object." in my
server control (ascx.cs file)
There is a table in my user control with id "tdCell".

I'm creating a Label control at runtime in Page_Load of user control,
and want to add it in the "tdCell" by just tdCell.Controls.Add()....

There is a Panel on my aspx page with id "TBarPanel", and want to add
the user control at runtime. but i m getting error.

Any idea...I tried panel/placeholder/table every thing but it's not
working.
I also tried using LoadControl("MyControl.ascx"), but it's also not
working.
I put all code and error string for your reference.

Thanks & Regards,
Rushikesh


////////// MyControl.ascx /////////////
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="MyControl.ascx.cs" Inherits="MyControl" %>

<table cellpadding="0" cellspacing="0">
<tr>
<td id="tdCell" runat="server"></td>
</tr>
</table>


////////// MyControl.ascx.cs /////////////

using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class MyControl: System.Web.UI.UserControl
{

protected void Page_Load(object sender, EventArgs e)
{
Label lbl = new Label();
lbl.Text = "Hello";
tdCell.Controls.Add(lbl);

}
}
//// //// MyControl_Test.aspx /////////

<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="MyControl_Test.aspx.cs" Inherits="MyControl_Test" %>

<%@ Register Src="MyControl.ascx" TagName="MyControl" TagPrefix="uc1"
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="TBarPanel" runat="server" Height="42px"
Width="100%">
</asp:Panel>
</form>
</body>
</html>

//// //// MyControl_Test.aspx.cs /////////

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class TimeBarCtrl_Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MyControl myctrl1 = new MyControl();
placeUserControls.AddTimeBarSegment(myctrl1);

MyControl myctrl2 = new MyControl();
placeUserControls.AddTimeBarSegment(myctrl2);

MyControl myctrl3 = new MyControl();
placeUserControls.AddTimeBarSegment(myctrl3);

}
}

////////////////////**********************Error

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:
Line 54: Label lbl = new Label();
Line 55: lbl.Text = "Hello";
Line 56: tdCell.Controls.Add(lbl);
Line 57:
Line 58:
Source File: d:\inetpub\wwwroot\MACT\Interface\TimeBarCtrl.ascx .cs
Line: 56

Stack Trace:
[NullReferenceException: Object reference not set to an instance of an
object.]
TimeBarCtrl.Page_Load(Object sender, EventArgs e) in
d:\inetpub\wwwroot\MACT\Interface\TimeBarCtrl.ascx .cs:56
System.Web.Util.CalliHelper.EventArgFunctionCaller (IntPtr fp, Object
o, Object t, EventArgs e) +13
System.Web.Util.CalliEventHandlerDelegateProxy.Cal lback(Object
sender, EventArgs e) +45
System.Web.UI.Control.OnLoad(EventArgs e) +80
System.Web.UI.Control.LoadRecursive() +49
System.Web.UI.Control.LoadRecursive() +132
System.Web.UI.Control.LoadRecursive() +132
System.Web.UI.Control.LoadRecursive() +132
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+3710

Mar 30 '06 #1
4 1950
More code would be helpful.

It should look something:

Control c = Page.LoadControl("controlFileName.ascx");
tdCell.Controls.Add(c);

From the brief description you've given, it looks like the error might
actually be happening in your contorl (you say the object reference is
occuring in an ascx file) so it's really hard to tell what exactly is going
on.

Karl
--
http://www.openmymind.net/
http://www.fuelindustries.com/
<ru*************@gmail.com> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
Hi All,

I have created my own WebControl and want to add it in my aspx page at
runtime.

it's compiling perfectly, but when i m going to execute, it gives me
error of "Object reference not set to an instance of an object." in my
server control (ascx.cs file)
There is a table in my user control with id "tdCell".

I'm creating a Label control at runtime in Page_Load of user control,
and want to add it in the "tdCell" by just tdCell.Controls.Add()....

There is a Panel on my aspx page with id "TBarPanel", and want to add
the user control at runtime. but i m getting error.

Any idea...I tried panel/placeholder/table every thing but it's not
working.
I also tried using LoadControl("MyControl.ascx"), but it's also not
working.
I put all code and error string for your reference.

Thanks & Regards,
Rushikesh


////////// MyControl.ascx /////////////
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="MyControl.ascx.cs" Inherits="MyControl" %>

<table cellpadding="0" cellspacing="0">
<tr>
<td id="tdCell" runat="server"></td>
</tr>
</table>


////////// MyControl.ascx.cs /////////////

using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class MyControl: System.Web.UI.UserControl
{

protected void Page_Load(object sender, EventArgs e)
{
Label lbl = new Label();
lbl.Text = "Hello";
tdCell.Controls.Add(lbl);

}
}
//// //// MyControl_Test.aspx /////////

<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="MyControl_Test.aspx.cs" Inherits="MyControl_Test" %>

<%@ Register Src="MyControl.ascx" TagName="MyControl" TagPrefix="uc1"
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="TBarPanel" runat="server" Height="42px"
Width="100%">
</asp:Panel>
</form>
</body>
</html>

//// //// MyControl_Test.aspx.cs /////////

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class TimeBarCtrl_Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MyControl myctrl1 = new MyControl();
placeUserControls.AddTimeBarSegment(myctrl1);

MyControl myctrl2 = new MyControl();
placeUserControls.AddTimeBarSegment(myctrl2);

MyControl myctrl3 = new MyControl();
placeUserControls.AddTimeBarSegment(myctrl3);

}
}

////////////////////**********************Error

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:
Line 54: Label lbl = new Label();
Line 55: lbl.Text = "Hello";
Line 56: tdCell.Controls.Add(lbl);
Line 57:
Line 58:
Source File: d:\inetpub\wwwroot\MACT\Interface\TimeBarCtrl.ascx .cs
Line: 56

Stack Trace:
[NullReferenceException: Object reference not set to an instance of an
object.]
TimeBarCtrl.Page_Load(Object sender, EventArgs e) in
d:\inetpub\wwwroot\MACT\Interface\TimeBarCtrl.ascx .cs:56
System.Web.Util.CalliHelper.EventArgFunctionCaller (IntPtr fp, Object
o, Object t, EventArgs e) +13
System.Web.Util.CalliEventHandlerDelegateProxy.Cal lback(Object
sender, EventArgs e) +45
System.Web.UI.Control.OnLoad(EventArgs e) +80
System.Web.UI.Control.LoadRecursive() +49
System.Web.UI.Control.LoadRecursive() +132
System.Web.UI.Control.LoadRecursive() +132
System.Web.UI.Control.LoadRecursive() +132
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+3710

Mar 30 '06 #2
Hi Karl,

Thankyou for quick response.

Yes you are right, there might be some problem in my user control.

But it seems ok, there is tdCell id and my MyControl.ascx page and i m
trying to add some label control by just tdCell.Controls.Add(lbl).

this is working perfectly in ASPX file but this is not working in ASCX.

Thanks
Rushikesh

Mar 30 '06 #3
I've tested your basic code and it works just fine but you'll need to
do a bit more work to instantiate a user control programmatically.

More details here.
http://msdn2.microsoft.com/en-us/library/c0az2h86.aspx

Mar 30 '06 #4
Looks like tdCell is null.

--
Patrice

<ru*************@gmail.com> a écrit dans le message de news:
11*********************@i40g2000cwc.googlegroups.c om...
Hi Karl,

Thankyou for quick response.

Yes you are right, there might be some problem in my user control.

But it seems ok, there is tdCell id and my MyControl.ascx page and i m
trying to add some label control by just tdCell.Controls.Add(lbl).

this is working perfectly in ASPX file but this is not working in ASCX.

Thanks
Rushikesh

Mar 30 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Wayno | last post by:
My php logs are coming up empty. I have done all I can think of, and all that made sense to me. Can someone take a look at my php.ini please and tell me what you think may be the problem. I...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
16
by: TD | last post by:
This is the code under a command button - Dim ctl As Control For Each ctl In Me.Controls If ctl.BackColor <> RGB(255, 255, 255) Then ctl.BackColor = RGB(255, 255, 255) End If Next ctl
1
by: Michael | last post by:
Hi, Any one know the problem of the following error: Thanks, m Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to...
9
by: JTrigger | last post by:
When I compile my project using the IDE on a development machine it works just fine. When I compile it on the server using csc.exe, I get the following error when I try to bring it up in the web...
2
by: Jeremy | last post by:
Everything in my app worked fine - then I moved some user controls to a subfolder named \ControlTemplates that physically exists directly under the site root. Then, at runtime when these lines...
2
by: Sam Kuehn | last post by:
There has been a lot of articles on how to load user controls at runtime in the Init() method. UserControl myControl = (UserControl)LoadControl(stringControl); I add the control in the Init()...
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
2
by: Chris | last post by:
I am getting a viewstate error when adding dynamically user controls. SYSTEM_EXCEPTION:Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.