views:

251

answers:

3

Hi, I've posed this problem previiously also here. I'm facing same type of problem again. I've a user control that shows information from some business object. I'm already using it on two pages. In both the pages I call a method with business object to bind controls (mostly asp labels) on user control to object's properties. I call this method in LoadComplete method of aspx page.

  protected void Page_LoadComplete(object sender, EventArgs e)
  {
      control.LoadData(bussinessObject);
  }

It is working fine on both the pages. Now I've third page where I'm using the same control. Now when I call LoadData in this new page I'm getting NullReference exception:

 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 91:             lblOrgName.Text = obj.Name;

EDIT: lblOrgName is NULL :( Please help me!

EDIT 2: I stepped through the code. After Page Load of the page containing page, Page_Load of Masterpage is called and then the problem control's Page Load is called. Stopping at closing brace (}) of this Page Load function I typed lblOrgName in Immediate Window and it show null :(

Then in LoadComplete of the page containing the user control I stopped and typed the control's name in immediate window. It shows all the labels and other control's in it as null.

EDIT 3:

I've put protected constructor in the UserControl as suggested by Stendhal in the answers:

 protected OrgInfo(){ }

Now I'm getting this error:

 Compiler Error Message: CS0122: 'Controls.OrgInfo.OrgInfo()' is inaccessible 
             due to its protection level.

The aspx page in the root directory of the project and the OrgInfo control is in Controls folder.

EDIT 4:

I tried to create all the controls (mostly labels) in page load method but alas the count of Controls collection is 0 (zero) !

A: 

It sounds as though the control "lblOrgName" is attempting to be assigned a value but it hasn't yet been built/resolved into the aspx page.

Make sure in the @page directive of the problematic aspx page that you have the following set:

AutoEventWireup="true"
danrichardson
yes it is. Page Language="C#" AutoEventWireup="true"
TheVillageIdiot
+1  A: 

How do you put your usercontrol on the page? This has happened to me in the past and the reason was that control was accidently added as a server control and not a user control. All the subcontrols in the control were never instatiated.

You can check this by putting a protected default constructor in the code-behind for the user control:

protected MyUserControl() { }

That way the control cannot be instantiated as a server control.

Per Erik Stendahl
I've edited the question. Please any further suggestion? I'm putting it like this <Proj1:OrgInfo id="orgInfo" runat="server" />
TheVillageIdiot
Do you have any mappings in web.config (configuration/system.web/pages/controls/)?
Per Erik Stendahl
Yes, I've put line in <controls> portion: <add tagPrefix="proj1" namespace="Proj1.Controls" assembly="Proj1"/>
TheVillageIdiot
your comment gave me idea that it may be the reason!
TheVillageIdiot
Glad it works now. A suggestion: use different tag prefixes for user controls and server controls. That way you won't risk mixing them up. :-)
Per Erik Stendahl
A: 

I put following line in page:

 <%@ Register src="~/Controls/OrgInfo.ascx" TagName="OrgInfo" TagPrefix="proj1"/>

and it works!

TheVillageIdiot