Instantiating Custom Control - Object reference not set to an instance of an object

Hello all, first time poster here.

 

I have created a custom control that I am trying to add on my page programmatically.  If I write the asp markup for the control on the .aspx file, it works fine.  However, when I try to add text to the control's properties using c#, it throws the object reference error mentioned above.  For example:

 

CustomControl mycontrol = new CustomControl();    

 mycontrol.bodytext = "lorem ipsum....";

PlaceHolder1.Controls.Add(mycontrol);

----------------

 Bodytext is a property of the CustomControl class and appears in intellisense, further, I can define the property in the asp markup as well.  It's just whenever I try to manipulate the bodytext (or any other property) of the control instance that this error is thrown.

 

Anyone have any advice? 

 

Thank you!

0
dyermaker221
3/5/2009 3:18:40 PM
asp.net.web-forms 93655 articles. 6 followers. Follow

8 Replies
940 Views

Similar Articles

[PageSpeed] 15
Get it on Google Play
Get it on Apple App Store

 Also, I'm not sure if this helps at all, but no error is thrown if I just instantiate the class and add it to the placeholder without using any of the properties.

0
dyermaker221
3/5/2009 4:17:02 PM

Hi,

is the bodytext property mapped to a child control property?

If so, try inverting the code lines:

 

PlaceHolder1.Controls.Add(mycontrol);

mycontrol.bodytext = "lorem ipsum....";

 

Hope this helps. Bye!


=WereWolf=
--
MCPD
--
Helping someone does not mean giving them what they need, but making them able to get it themselves
--
The Code Golem http://www.codegolem.com/
0
WereWolf
3/5/2009 4:19:39 PM

 By child control property, do you mean a control within the custom control? In my example, bodytext is a property in the class and it maps to an html control inside my custom control.  So in my property I have the value of bodytext setting the value of the html control.

 

I also tried inverting the lines like you said already, but to no avail.

 

Thank you

0
dyermaker221
3/5/2009 4:23:29 PM

Could you please tell us when in your page's lifecycle you are adding the control to the placeholder?

And when in the custom control's lifecycle is the child html control instanciated?

 

Thank you.


=WereWolf=
--
MCPD
--
Helping someone does not mean giving them what they need, but making them able to get it themselves
--
The Code Golem http://www.codegolem.com/
0
WereWolf
3/5/2009 4:34:36 PM

In the Page_Load.

 

In the custom control I have not instantiated the html in the code behind.  Bodytext for example is an html <p> tag that looks like <p id="bodytext" runat="server"> and I have my property in the custom control populating the innertext attribute of bodytext.  Do I need to instantiate every control in the codebehind first?

 

Sorry, I am new at using custom controls in this fashion.

 

Thanks again.

0
dyermaker221
3/5/2009 4:51:39 PM

You were talking about "Custom Control", I assumed you were instantiating child controls by code.

What you refer to actually is a User Control (you have an .ascx file, is this right?)

User Controls may not be instantiated simply by their constructor. You have to Load the control:

 

CustomControl mycontrol = (CustomControl)LoadControl("UrlToMyCustomControl.ascx");

mycontrol.bodytext = "lorem ipsum....";

PlaceHolder1.Controls.Add(mycontrol);

  

 

This should solve your issue Smile
=WereWolf=
--
MCPD
--
Helping someone does not mean giving them what they need, but making them able to get it themselves
--
The Code Golem http://www.codegolem.com/
0
WereWolf
3/5/2009 5:24:09 PM

 I see! I will try this when I get a chance and let you know.  I would bet that this solves my issue.  Yes, I do have a .ascx file.

 

If it is not too much of a bother, could you point me in the direction of someplace where I can learn the difference between a user and custom control? I always thought .ascx files were custom controls.

 

Thanks again

 

 

Edit:  Nevermind, I found a good site.  

0
dyermaker221
3/5/2009 5:45:25 PM

Here is a description of User Controls

http://msdn.microsoft.com/en-us/library/y6wb1a0e.aspx

Here you can find a walkthrough on creating and using Custom Controls

http://msdn.microsoft.com/en-us/library/yhzc935f.aspx

 

Good luck!


=WereWolf=
--
MCPD
--
Helping someone does not mean giving them what they need, but making them able to get it themselves
--
The Code Golem http://www.codegolem.com/
0
WereWolf
3/5/2009 5:55:29 PM
Reply:

Similar Artilces:

Controlling web control instances on Web Form
I have created a composite control, extending functionality of the Hidden Control. However, I would like to prevent the user from adding this control to the page multiple times. I only want one instance of the control on the Web Form. What setting in my project to I adjust to enable this functionality? I can not find it in any of the books that I have referenced. Is this a setting or is this code that has to be added to the inheriting class. Looks like there is no standard apporach. But I'd consider using static field to count number of created objects and throw exception if second object ...

Web Custom Control within a Web Custom Control
I was wondering if this can be done and how. I've created a simple custom control that displays a header which is my first custom control. So I'm just a baby when it comes to this.. Within the header I currently have a property for breadcrumbs which is a string. What I would like to do is have the breadcrumb property  be a Link Button List. I did find a custom control of a Link Button List which is exactly what I wanted. My question is how can/do  I include this into my custom control? Can it done? How? Please help? I'm at a lose here.   Susan You should be able to do ...

Custom Web Control inside another Custom Web Control
Hi I created a custom web control (CusWebNew) that displays some textboxes by adding them to the ControlCollection of the webcontrol. I also created a custom webcontrol (CustWebButton) that inherts from Button control, and overrided the OnClick function. When I use CustWebButton on a regular web form, it seems to function correctly. However, when I added to be part of the ControlCollection of CustWebNew, it stops to work.   Any ideas why? Thanks Hey, What do you mean by stops to work?  Explain what you are trying to do with it and what it doesn't do.Brian"Trust in the...

custom web user control and web form
Guys,          I am trying to develop a context menu control for all of my pages. what i am trying to do is that i want the control to pick a datasource from the markup and then check whether its an xml file or a datatable. I created a class called datasource.   /**************************DataSource Class****************************/  public class DataSource { /// <summary> /// This is to identify unique elements from the markup /// </summary>protected string _ID; /// <summary> /// This will be the Datasource ID e...

Add .Net Control to Web Custom Control
 I want to add .NET Control (like: TextBox, Button...) to Web Custom Control.How i can do it? Alphavn That's called a composite control -- "Mark As Answer" if my reply helped you -- Hello Guys What gunteman said, that is one approach and in most cases the right one.But its also possible to add child controls to a Control thats inherit from WebControl itself. Be sure to implement INamingContainer to that inherited Control too, thats the important point of that approach.--Microsoft MVP - Visual Developer ASP / ASP.NET- http://www.aspnetzone.de/blogs/peterbuch...

I cannot drag web custom controls from toolbox to web form
I have created some web custom controls (both composite controls or derived/inherited controls).  I successfully added them to the toolbox. However, when I try to drag them onto the form, the cursor changes into a black circle with a diagonal line, and cannot drag them.  I also tried double-clicking them from the toolbox but it still won't work. I sent the Control Library (DLL) to a friend, and he managed to use it properly.  I am assuming this is a problem with my Visual Studio. Has anyone encountered such a problem?  Any suggestions or solutions?   Thanks. I...

Cant drag-and-drop Web Custom Control to Web Form!
Hi,I'm going insane here, I've read countless articles how to create a Web Custom Control and add it to the Toolbox and then just Drag-and-Drop it to a Web Form.And I have done exactly that except for the last step, any Web Custom Control I create refuses to be Drag-and-Dopped to the Web Form. What happens is the following, after I have added the Web Custom Control to the ToolBox I click it and drag it to the Web Form designer area. First I can see the icon of the Web Custom Control and I see a Referance being created but just after that the icon changes to one of those "you cant park here s...

User Control VB.Net x Web Custom Control
I was thinking in create a user control in VB.Net, and use it in a Asp.Net as possible? So I thought about it is gonna be better than make a Web Custom Control, so i thought again, nope... But i need to know better because a don't have a progress bar in asp.net for example.... Could Somebody tell me about??? in a reallity i know the asnwer but i would like to explain by myself... Thank's Antonio...

Reusing custom web control in the same web control
Hello, I'm newbie in web development and of course asp.net. I am actually trying to build my first page and I'd like to know if it is possible to make a custom webcontrol that will contain itself. Let's say I want to draw a folder hierarchy in treeview-like structure. I thought I could make a control that will display a folder and if this folder contains subfolders then for each the control will include itself. Is this even possible? What would you suggest me to do to? Thanks in advance. TomTom Hagen http://thagen.dteam.info If the control can have child controls, then I s...

How to set tabindex to the controls if the total number of the controls on a web form is not sure?
Hi,I have a web form which has a multiview control along with other web controls. Each view of this multiview control has different number of web controls. So if user selects 1. the first view to load then the form has total 10 controls on it (5 control of the form, and 5 controls of the view selected by user)2. the second view to load then the form has total 8 controls on it (5 control of the form, and 3 controls of the view selected by user)3. the third view to load then the form has total 20 controls on it (5 control of the form, and 15 controls of the view selected by user)As i dont kn...

How to access a control on a web form from anoter web form
hi all I have this scenarios i have two web forms, one called form1 and the other called form2 form1 has a label with id "lbl1" is it possible to access lbl1 from form2, change its text permenantly? thanks.   refer: http://dotnetslackers.com/Community/blogs/haissam/archive/2007/11/26/ways-to-pass-data-between-webforms.aspxSSNPlease remember to click "Mark as Answer" on the post that helps you. MinaSamy: hi all I have this scenarios i have two web forms, one called form1 and the other called form2 form1 has a label with id "lbl1" is it p...

What is the difference between a Web User Control and a Web Custom Control
Hi ,please could you answer for the following question with example. i know wht they are. but i want to know hw they are existing in web pages with simple example. What is the difference between a Web User Control and a Web Custom Control?  Thanks in AdvanceDilip    Check this out .... hope these might help you out... http://support.microsoft.com/kb/893667http://msdn2.microsoft.com/en-us/library/aa651710(VS.71).aspx itsUrDAY  Hi, See the following URLs: https://msdn2.microsoft.com/en-us/library/fb3w5b53(VS.71).aspx(Introduction to Web User Contr...

Custom User Controls v. Custom Web Controls
Hi! Is it better for me to begin coding my controls instead of just designing them? What would the benefits be? I meen, i seem to be able to get all i need from just putting stuff together on an ASCX file but i'm obviously missing a huge concept here... please explain! Hi, creating user controls is indeed easier than creating custom server controls because you can simply drag & drop other controls on the form, easily add events to it, ... Custom controls however are better when you want to reuse them in several other projects. I like to create custom controls for small, reusable com...

Server Web User Control or Web Custom Control
Ok, I made a DataGrid I like, stuck it in a Web Control Lib, and then to my toolbar. Now I want to take my nice little control and add it to a greater control, maybe place some buttons around it etc. I could always inherit from it in a new custom control and dynamically add the buttons, but I thought about using a Web User Control. I thought I could easily create a Web User Control, drop on my grid and the buttons on a form, and expose some properties and events, but once I got into it, I got a feeling that this isn't what a Web User Control is for. When designing a control li...

Use Web Custom control or Web User control?
Friends,  I have a generic aspx page and its functions which is used in many .NET web application. The generic page contains a HTML Div control, server drop down list, server buttons, server textbox and it have different server and client functions to do its tasks for calling aspx pages in web applications. My problem is I do not know whether I should make that generic page as a web user control (but this is tedious because all other application folders must store frontpage aspx and code behind files of the generic page) or as a web custom control (This seems to be good beca...

Web resources about - Instantiating Custom Control - Object reference not set to an instance of an object - asp.net.web-forms

Facebook SDK for PHP - Facebook-Entwickler
The Facebook SDK for PHP provides a rich set of server-side functionality for accessing Facebook’s s...

New Jersey State Constitution - Wikipedia, the free encyclopedia
The Constitution of the State of New Jersey is the basic governing document of the State of New Jersey . In addition to three British Royal Charters ...

Time zone - Wikipedia, the free encyclopedia
This article is about time zones in general. For a list of time zones by country, see List of time zones by country . For more time zone lists, ...

Simplest Java decoupling without 3rd party frameworks
From Wikipedia In object-oriented programming, removing dependencies is done by using interface. Thus, if class A is dependent on class B, we ...

Ajay Matharu on Web, Technology and Life
... home about integrating blogengine into existing site tweet old post Ajay+ Fundamental Provocation Understanding declarative class and instantiating ...

'Chronicle' staff past and present on 20 years of covering SXSW Film
'Chronicle' staff past and present on 20 years of covering SXSW Film

Advanced dependency injection with Guice
... any Guice tutorial you can find on the net . Once the injector has created your graph of objects, you are pretty much back to normal and instantiating ...

New World Notes
Wagner James Au reports on Second Life and other virtual worlds, and related topics, such as virtual goods, next gen gaming, and new immersive ...

SAP Runs VMware to Provision Virtual Machines to Support Complex Training Courses
Transcript of a BriefingsDirect podcast on how SAP uses VMware products to implement a private cloud that smooths out educational apps runtime ...

Developing Intelligence - The Old Version
Several researchers at Northwestern have created a hybrid neurorobotic system, consisting of a brain-machine interface between a Khepera base ...

Resources last updated: 12/13/2015 12:17:22 PM