I’m probably not the first who has this problem, but I can’t find such question and solution for it.
I have a base .aspx page and child page which inherits from the base page.
From child’s Page_Load method I call a method on a base page like base.SetLiteral(value) which acceses a literal defined in the base page. I get a NullReferenceException, as the literal is null.
It probably has to do with page lifecycle as base page’s controls are not instansiated at that point yet.
How can I do it?
EDIT
Here is a stack trace of the exception. Doesn’t say anything to me.
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=PageInheritance
StackTrace:
at PageInheritance.BasePage.SetLiteral(String value) in D:\crap-projects\PageInheritance\BasePage.aspx.cs:line 17
at PageInheritance.Page1.Page_Load(Object sender, EventArgs e) in D:\crap-projects\PageInheritance\Page1.aspx.cs:line 13
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
Ok, it looks like when one inherits an .aspx page it’s only the code behind which is being inherited. The markup ( .aspx itself) is not inherited and there is no way (like ContentPlaceHolder in a Master page) to define what elements (from derived, base page or both) one wants to be on the result page.
As base .aspx page is not inherited no controls added to it in designer ever initialized. I don’t really understand how it works though (Page_Init is called but how page decides not to initialize its controls is a mistery) Can anyone explain this bit?
I will probably have to use Master page to achieve my goal, but I will continue my research and update my question.
If I’m mistaken somewhere please let me know.
EDIT
Yes, I achieved what I needed to do using Master page instead of .aspx page inheritance. Did not have any problems there.
P.S.
I did find a way to inherit an .aspx page having markup in base page here: http://www.codeproject.com/KB/aspnet/page_templates.aspx but it looks like complete hackery to me.