Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2006

Tip: Looking for answers? Try searching our database.

error in adding user controls at runtime

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
rushikesh.joshi@gmail.com - 30 Mar 2006 14:40 GMT
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.Callback(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
Karl Seguin [MVP] - 30 Mar 2006 14:51 GMT
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

Signature

http://www.openmymind.net/
http://www.fuelindustries.com/

> Hi All,
>
[quoted text clipped - 149 lines]
> includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
> +3710
rushikesh.joshi@gmail.com - 30 Mar 2006 15:01 GMT
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
CaffieneRush@gmail.com - 30 Mar 2006 15:22 GMT
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
Patrice - 30 Mar 2006 15:26 GMT
Looks like tdCell is null.

Signature

Patrice

> Hi Karl,
>
[quoted text clipped - 9 lines]
> Thanks
> Rushikesh

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.