Yow to Generate Unique Control ID?

Discussion in 'ASP .Net' started by Jeremy, Jun 16, 2008.

  1. Jeremy

    Jeremy Guest

    I am dynamically inserting multiple instances of a custom Web server control
    into a page. After inserting the control, I use FindControl() to locate the
    just-inserted control, then set some properties on the control.

    In order for this to work, the custom Web server control that I'm inserting
    must have a unique ID value within the hosting page.

    I'm currently [and temporarily] simply appending a unique integer value to
    the control ID value. The unique integer is a static member of the control
    class. And this is a problem, because it increments *every* time the control
    is inserted - across sessions + page requests. Clearly this is an
    unacceptable situation, as the integar value would increment 'til an
    overflow exception... unless I reset the value... but I don't want this
    entire setup.

    So, I'd appreciate some suggestions for generating a unique ID value for the
    inserted control instances. I don't want a string-representation of a GUID,
    as that's way too long and overkill.

    The value needs to be unique only for a given instance of the page. And it's
    very unlikely that there would ever be more than five instances of the
    control.

    FWIW, the control is an abstraction of a menu, site map, treeview, and
    breadcrumbs. Once the control is inserted on the page, it decides which of
    those specialized representations it needs to render as. So on a given page,
    it is likely that I'd have 3-5 of those controls, and probably never more
    than 6 (assuming multiple menus can be inserted along with the site map and
    breadcrumbs).

    Thanks.
     
    Jeremy, Jun 16, 2008
    #1
    1. Advertising

  2. Jeremy

    Hans Kesting Guest

    Jeremy wrote :
    > I am dynamically inserting multiple instances of a custom Web server control
    > into a page. After inserting the control, I use FindControl() to locate the
    > just-inserted control, then set some properties on the control.
    >
    > In order for this to work, the custom Web server control that I'm inserting
    > must have a unique ID value within the hosting page.
    >
    > I'm currently [and temporarily] simply appending a unique integer value to
    > the control ID value. The unique integer is a static member of the control
    > class. And this is a problem, because it increments *every* time the control
    > is inserted - across sessions + page requests. Clearly this is an
    > unacceptable situation, as the integar value would increment 'til an overflow
    > exception... unless I reset the value... but I don't want this entire setup.
    >
    > So, I'd appreciate some suggestions for generating a unique ID value for the
    > inserted control instances. I don't want a string-representation of a GUID,
    > as that's way too long and overkill.
    >
    > The value needs to be unique only for a given instance of the page. And it's
    > very unlikely that there would ever be more than five instances of the
    > control.
    >
    > FWIW, the control is an abstraction of a menu, site map, treeview, and
    > breadcrumbs. Once the control is inserted on the page, it decides which of
    > those specialized representations it needs to render as. So on a given page,
    > it is likely that I'd have 3-5 of those controls, and probably never more
    > than 6 (assuming multiple menus can be inserted along with the site map and
    > breadcrumbs).
    >
    > Thanks.


    Do you know about the INamingContainer interface?
    Implementing this at the level of a composite control (easy - it has no
    members you need to implement) makes sure the subcontrols of that
    composite control get a unique prefix (when the ClientId is calculated
    from the ID you have specified).

    Hans Kesting
     
    Hans Kesting, Jun 17, 2008
    #2
    1. Advertising

  3. Hello Hans

    > Implementing this at the level of a composite control (easy - it has no

    The class "CompositeControl" allready implements the interface
    INamingContainer.
    But the interface can implement on all components that derivating from the
    Control class.

    --
    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], Jun 18, 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. Ronald
    Replies:
    6
    Views:
    7,078
    Andy Mortimer [MS]
    Feb 23, 2004
  2. Max
    Replies:
    5
    Views:
    32,820
    Sudsy
    Feb 28, 2004
  3. ToshiBoy
    Replies:
    6
    Views:
    885
    ToshiBoy
    Aug 12, 2008
  4. Phil Carmody

    yow

    Phil Carmody, Oct 23, 2008, in forum: C Programming
    Replies:
    7
    Views:
    499
    Richard Bos
    Oct 27, 2008
  5. Token Type
    Replies:
    9
    Views:
    391
    Chris Angelico
    Sep 9, 2012
Loading...

Share This Page