Templated Custom Controls

N

Nick

Hi,

I am developing a set of custom templated controls to clean up and unify the
design of our ASP.NET website. The controls are DataEntryPage,
DataEntrySection, DataEntryRow and DataEntryUnit, and are basically tables
with the appropriate CSS settings and whatever to keep it all nice and neat
and consistent.

My problem arises through the simple fact that when I place a DataEntryPage
(the outermost of the controls) into a UserControl, I get
NullReferenceExceptions when I try to access its controls through
server-side code. I have pinned this down to the fact that none of the
DataEntry controls have their CreateChildControls method called; the
constructors are called, but no CreateChildControls.

I can't understand why this is happening! Every other custom control I have
developed works perfectly as expected, what makes these ones different? The
simple fact that they have publically available ITemplate properties should
not cause this behaviour. So, I have created my own UserControl class which
overrides OnInit, and calls EnsureChildControls before I do anything else.

Sure enough, the CreateChildControls method of the UserControl is called,
but, for some unknown reason, the DataEntry units, and consequently their
contained controls, never have their CreateChildControls methods called.
Only with a manual search through UserControl.Controls for a DataEntryPage,
and then calling a publically available method called CreateControls (which
simply calls EnsureChildControls from within the DataEntryPage) on that
DataEntryPage, am I able to get the desired behaviour. Why?

Any help is appreciated.

Regards,
Nick
 
N

Nick

Hi,

Thanks for your reply. Since the original posting, I have progressed
significantly, but 'null' values still plague me. My markup looks like so:

<sm2:DataEntryPage ID="DataEntryPage1" runat="server">
<Header>Hello</Header>
<Sections>
<sm2:DataEntrySection ID="DataEntrySection1"
runat="server">
<Header>Hello</Header>
<Rows>
<sm2:DataEntryRow ID="DataEntryRow1"
runat="server">
<Units>
<sm2:DataEntryUnit ID="DataEntryUnit1"
runat="server">
<Label>
// NULL
<span
style="color:#FFFFFF">Hello</span>
</Label>
<Input>
// NULL
<asp:Button ID="Button1"
runat="server" Text="Button" />
</Input>
</sm2:DataEntryUnit>
</Units>
</sm2:DataEntryRow>
</Rows>
</sm2:DataEntrySection>
</Sections>
</sm2:DataEntryPage>

The Label and Input templates return null values in the CreateChildControls
method of the DataEntryUnit custom control. As in all examples I have seen,
my code looks a little like this:

protected override void CreateChildControls()
{
if (this.Label != null)
this.Label.InstantiateIn(this);

if (this.Input!= null)
this.Input.InstantiateIn(this);

base.CreateChildControls();
}

Now, looking at the generated HTML source code, the page works exactly how
it is supposed to, and on the server-side I have programmatic access to all
controls listed in the markup at any time, as I should (this was my original
problem, now fixed). The only thing is that if I set a break point in the
CreateChildControls method of the DataEntryUnit (as listed above), Label and
Input are null, and no controls are created inside the DataEntryUnit.

Why are they null? They have controls in them in the markup, and I can even
access them from the server-side! Any help is greatly appreciated.

Regards,
Nick

----- Original Message -----
From: "Peter Bucher [MVP]" <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
Sent: Friday, June 13, 2008 11:05 PM
Subject: Re: Templated Custom Controls
 
P

Peter Bucher [MVP]

Hello Nick

That could be, because CreateChildControls did invoke a few times.
And on the first invokes, the values could be null

Test this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top