Templated Custom Controls

Discussion in 'ASP .Net Web Controls' started by Nick, Jun 13, 2008.

  1. Nick

    Nick Guest

    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
    Nick, Jun 13, 2008
    #1
    1. Advertising

  2. 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:...
    > 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
    Peter Bucher [MVP], Jun 13, 2008
    #2
    1. Advertising

  3. Nick

    Nick Guest

    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:...
    >> 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

    >
    Nick, Jun 16, 2008
    #3
  4. 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:...
    > 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:...
    >>> 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

    >>

    >
    Peter Bucher [MVP], Jun 16, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RA Scheltema
    Replies:
    3
    Views:
    392
    RA Scheltema
    Jan 6, 2004
  2. Marijn
    Replies:
    5
    Views:
    452
    Marijn
    Feb 13, 2004
  3. Replies:
    0
    Views:
    2,226
  4. Amadeus W. M.
    Replies:
    2
    Views:
    390
    Amadeus W. M.
    Jul 4, 2006
  5. Replies:
    1
    Views:
    302
Loading...

Share This Page