|
|
|
date: Fri, 13 Jun 2008 13:47:27 +1000,
group: microsoft.public.dotnet.framework.aspnet.webcontrols
back
Templated Custom Controls
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
date: Fri, 13 Jun 2008 13:47:27 +1000
author: Nick
Re: Templated Custom Controls
Hello Nick
Which object is null, and at which time?
--
Gruss, Peter Bucher
Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
"Nick" wrote in message
news:C3DCC653-3039-4AE9-9EA4-D5A393DFC798@microsoft.com...
> 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
date: Fri, 13 Jun 2008 15:05:38 +0200
author: Peter Bucher [MVP]
Re: Templated Custom Controls
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]"
Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
Sent: Friday, June 13, 2008 11:05 PM
Subject: Re: Templated Custom Controls
> Hello Nick
>
> Which object is null, and at which time?
>
> --
> Gruss, Peter Bucher
> Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
> http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
> http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
> "Nick" wrote in message
> news:C3DCC653-3039-4AE9-9EA4-D5A393DFC798@microsoft.com...
>> 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
>
date: Mon, 16 Jun 2008 09:04:36 +1000
author: Nick
Re: Templated Custom Controls
Hello Nick
That could be, because CreateChildControls did invoke a few times.
And on the first invokes, the values could be null
Test this.
--
Gruss, Peter Bucher
Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
"Nick" wrote in message
news:F527E85F-63F7-4608-854F-646D8368FE05@microsoft.com...
> 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]"
> Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
> Sent: Friday, June 13, 2008 11:05 PM
> Subject: Re: Templated Custom Controls
>
>> Hello Nick
>>
>> Which object is null, and at which time?
>>
>> --
>> Gruss, Peter Bucher
>> Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
>> http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
>> http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
>> "Nick" wrote in message
>> news:C3DCC653-3039-4AE9-9EA4-D5A393DFC798@microsoft.com...
>>> 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
>>
>
date: Mon, 16 Jun 2008 18:55:32 +0200
author: Peter Bucher [MVP]
|
|