INamingContainer and ClientID for nested Composite Controls -Guidance needed

Discussion in 'ASP .Net Building Controls' started by Greg Woods, Mar 19, 2008.

  1. Greg Woods

    Greg Woods Guest

    I have created a series of Composite Controls, inheriting from
    CompositeControl, which of course implements INamingContainer. Below
    is a snippet of simplified code to show how I'm using these controls
    in the page.
    (The reason I've rolled my own tabcontrol, and why I've used my own
    string translation are out of scope of this question)


    <cc:TabControl ID="TabControl" runat="server">
    <cc:Tab Text="SITE_INC19" ID="TabMain" runat="server">
    <cc:LabelledWrapper ID="EndDateRow" runat="server"
    Text="SITE_INC307">
    <cc:DateControl ID="EndDate" Text="ENDDATE"
    format="EditNoLabel" runat="server" />
    <cc:HyperlinkControl
    NavigateURL="javascript:removeEndDate()" runat="server" Text="REMOVE" /
    >

    </cc:LabelledWrapper>
    </cc:Tab>
    </cc:TabControl>

    Since I give each nested control its' own unique ID, I have no need
    for the default naming of INamingContainer.

    Also, considering the DateControl, which will render a textbox, I
    would prefer the rendered textbox have the ID of the overall control,
    in this case "EndDate", instead of
    "TabControl_TabMain_EndDateRow_EndDate_EndDate_date". For the
    constituent controls which make up the DateControl, I don't care what
    ID they are given - in fact as they are mostly labels and such which I
    never need to access using javascript, I actually have no need for
    them to have a rendered ID.

    The problem with the default behaviour is that it is
    * not needed, as I give unique Ids where appropriate
    * means i have to have server side code to determine clientIds in my
    javascript, instead of being to simply set it to the id of the control
    - i.e. I cannot hardcode: document.getElementById("EndDate") into a
    static .js file.
    * bloated html - elements - many of which have no need for any id

    As I'm fairly new to .NET, and very new to creating controls, what is
    my best option? Other related posts suggest stick with the official MS
    way of doing it - despite the issues I mention. Do I drop the use of
    CompositeControl, and add lots of 'EnsureChildControls' calls in my
    code? I'm open to suggestions.

    Thanks in advance

    Greg Woods
     
    Greg Woods, Mar 19, 2008
    #1
    1. Advertising

  2. Greg Woods

    Teemu Keiski Guest

    Re: INamingContainer and ClientID for nested Composite Controls - Guidance needed

    You can override the rendering or you can use custom helper solutions such
    as IDOverride: http://www.wilcob.com/Demos/IDOverride/

    Getting the ID for scripting in server-side code isn't too hard, therefore
    controls have ClientID property.

    --
    Teemu Keiski
    AspInsider, ASP.NET MVP
    http://blogs.aspadvice.com/joteke
    http://teemukeiski.net

    "Greg Woods" <> wrote in message
    news:...
    >I have created a series of Composite Controls, inheriting from
    > CompositeControl, which of course implements INamingContainer. Below
    > is a snippet of simplified code to show how I'm using these controls
    > in the page.
    > (The reason I've rolled my own tabcontrol, and why I've used my own
    > string translation are out of scope of this question)
    >
    >
    > <cc:TabControl ID="TabControl" runat="server">
    > <cc:Tab Text="SITE_INC19" ID="TabMain" runat="server">
    > <cc:LabelledWrapper ID="EndDateRow" runat="server"
    > Text="SITE_INC307">
    > <cc:DateControl ID="EndDate" Text="ENDDATE"
    > format="EditNoLabel" runat="server" />
    > <cc:HyperlinkControl
    > NavigateURL="javascript:removeEndDate()" runat="server" Text="REMOVE" /
    >>

    > </cc:LabelledWrapper>
    > </cc:Tab>
    > </cc:TabControl>
    >
    > Since I give each nested control its' own unique ID, I have no need
    > for the default naming of INamingContainer.
    >
    > Also, considering the DateControl, which will render a textbox, I
    > would prefer the rendered textbox have the ID of the overall control,
    > in this case "EndDate", instead of
    > "TabControl_TabMain_EndDateRow_EndDate_EndDate_date". For the
    > constituent controls which make up the DateControl, I don't care what
    > ID they are given - in fact as they are mostly labels and such which I
    > never need to access using javascript, I actually have no need for
    > them to have a rendered ID.
    >
    > The problem with the default behaviour is that it is
    > * not needed, as I give unique Ids where appropriate
    > * means i have to have server side code to determine clientIds in my
    > javascript, instead of being to simply set it to the id of the control
    > - i.e. I cannot hardcode: document.getElementById("EndDate") into a
    > static .js file.
    > * bloated html - elements - many of which have no need for any id
    >
    > As I'm fairly new to .NET, and very new to creating controls, what is
    > my best option? Other related posts suggest stick with the official MS
    > way of doing it - despite the issues I mention. Do I drop the use of
    > CompositeControl, and add lots of 'EnsureChildControls' calls in my
    > code? I'm open to suggestions.
    >
    > Thanks in advance
    >
    > Greg Woods
     
    Teemu Keiski, Mar 19, 2008
    #2
    1. Advertising

  3. Re: INamingContainer and ClientID for nested Composite Controls - Guidance needed

    Hallo Greg

    You can use a simple trick to not render ClientIDs.

    -
    http://translate.google.com/transla...id-rendert.aspx&langpair=de|en&hl=de&ie=UTF-8
    (Translated -> Englisch)
    -
    http://www.aspnetzone.de/blogs/pete...in-webcontrol-das-keine-clientid-rendert.aspx
    (Original)

    --
    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
     
    Peter Bucher [MVP], Apr 1, 2008
    #3
    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. Maras
    Replies:
    1
    Views:
    3,130
    Cowboy \(Gregory A. Beamer\)
    Oct 15, 2003
  2. sleigh
    Replies:
    1
    Views:
    2,694
    sleigh
    Feb 12, 2004
  3. Alex Stevens
    Replies:
    0
    Views:
    412
    Alex Stevens
    Oct 19, 2004
  4. sal_scozzari

    ClientID INamingContainer problem

    sal_scozzari, Nov 8, 2007, in forum: .NET
    Replies:
    0
    Views:
    484
    sal_scozzari
    Nov 8, 2007
  5. Mike

    Composite vs non composite Controls

    Mike, Mar 10, 2005, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    266
    Sundararajan
    Mar 11, 2005
Loading...

Share This Page