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) !