How do I uniquely identify a control?

Discussion in 'ASP .Net' started by Alan Silver, Feb 24, 2005.

  1. Alan Silver

    Alan Silver Guest

    Hello,

    After much battling, I think I am finally seeing some progress on my
    first user control (thanks to the patient souls who have answered my
    many questions!!). I am left with one more problem though...

    As detailed elsewhere, I have identified the need to use the ViewState
    to hold a flag that indicates whether or not this is the first time the
    control is loaded (ie if the flag isn't in the ViewState then it's the
    first time, otherwise it's not the first time). I was going to do this
    with something like ViewState["NotFirstTime"] and give it a value "y".

    This led me to wonder what happens if I use two instances of the control
    on a page. Presumably the first one to be loaded will set the ViewState
    value, and the second will read this and think it has already been
    loaded.

    That led me to think of using the control's ID instead, so that it would
    be uniquely identified. However, if I create a user control (which could
    simply be some HTML, the ASP.NET equivalent of a server-side include
    file) and that contains an instance of my control called (say) "fred",
    then there is nothing to stop me adding an instance of the same control
    with the same name to the main page. There wouldn't be a clash as the
    fred inside the user control would not be visible directly to the main
    page. Trouble is, this then causes a clash in the ViewState, as there
    would be two controls looking at ViewState["fred"].

    So (after all that waffle), is there a way of uniquely identifying a
    control in the entire page hierarchy? I assume that there will be some
    ID that the ASP.NET engine uses to identify controls. Presumably these
    would be unique, and would be preserved across postbacks. Can I get at
    this to make my ViewState identifier unique? Or is there a better way of
    doing this?

    Any advice appreciated. TIA

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
    Alan Silver, Feb 24, 2005
    #1
    1. Advertising

  2. Every Control has its own ViewState. :)

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    Neither a follower nor a lender be.

    "Alan Silver" <> wrote in message
    news:p...
    > Hello,
    >
    > After much battling, I think I am finally seeing some progress on my first
    > user control (thanks to the patient souls who have answered my many
    > questions!!). I am left with one more problem though...
    >
    > As detailed elsewhere, I have identified the need to use the ViewState to
    > hold a flag that indicates whether or not this is the first time the
    > control is loaded (ie if the flag isn't in the ViewState then it's the
    > first time, otherwise it's not the first time). I was going to do this
    > with something like ViewState["NotFirstTime"] and give it a value "y".
    >
    > This led me to wonder what happens if I use two instances of the control
    > on a page. Presumably the first one to be loaded will set the ViewState
    > value, and the second will read this and think it has already been loaded.
    >
    > That led me to think of using the control's ID instead, so that it would
    > be uniquely identified. However, if I create a user control (which could
    > simply be some HTML, the ASP.NET equivalent of a server-side include file)
    > and that contains an instance of my control called (say) "fred", then
    > there is nothing to stop me adding an instance of the same control with
    > the same name to the main page. There wouldn't be a clash as the fred
    > inside the user control would not be visible directly to the main page.
    > Trouble is, this then causes a clash in the ViewState, as there would be
    > two controls looking at ViewState["fred"].
    >
    > So (after all that waffle), is there a way of uniquely identifying a
    > control in the entire page hierarchy? I assume that there will be some ID
    > that the ASP.NET engine uses to identify controls. Presumably these would
    > be unique, and would be preserved across postbacks. Can I get at this to
    > make my ViewState identifier unique? Or is there a better way of doing
    > this?
    >
    > Any advice appreciated. TIA
    >
    > --
    > Alan Silver
    > (anything added below this line is nothing to do with me)
    Kevin Spencer, Feb 24, 2005
    #2
    1. Advertising

  3. Alan Silver

    Guest

    Alan Silver wrote:

    > So (after all that waffle), is there a way of uniquely identifying a
    > control in the entire page hierarchy? I assume that there will be

    some
    > ID that the ASP.NET engine uses to identify controls. Presumably

    these
    > would be unique, and would be preserved across postbacks. Can I get

    at
    > this to make my ViewState identifier unique? Or is there a better way

    of
    > doing this?
    >
    > Any advice appreciated. TIA
    >
    > --
    > Alan Silver
    > (anything added below this line is nothing to do with me)


    Yes, ASP.NET generates id's to make sure they are unique in cases like
    this.
    Google this group on "generated id" and "ClientID" and you should get
    an idea of how to use this id and when it's available.
    You can also set the ID property of a control with an arbitrary value
    to help manage them.

    HTH,
    Jim
    , Feb 24, 2005
    #3
  4. Alan Silver

    Alan Silver Guest

    >Every Control has its own ViewState. :)

    Duh, I thought it was one big ViewState shared between all controls on
    the page!!

    Ho hum, I've just spent ages playing with ClientID to get this working.
    Oh well, I might leave it now, if it ain't broke, don't fix it ;-)

    Thanks for the elucidation

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
    Alan Silver, Feb 24, 2005
    #4
  5. Alan Silver

    Alan Silver Guest

    >Yes, ASP.NET generates id's to make sure they are unique in cases like
    >this.
    >Google this group on "generated id" and "ClientID" and you should get
    >an idea of how to use this id and when it's available.
    >You can also set the ID property of a control with an arbitrary value
    >to help manage them.


    Thanks, what I didn't realise (until after I posted) was that the
    ClientID includes some hierarchy detail, so even in the case I
    mentioned, the two fred controls wouldn't have the same ClientID, one
    would just be called fred and one would be something like
    usercontrol1_fred.

    Thanks for the reply.

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
    Alan Silver, Feb 24, 2005
    #5
  6. I thought that would make you feel better. :)

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    Neither a follower nor a lender be.

    "Alan Silver" <> wrote in message
    news:y$...
    > >Every Control has its own ViewState. :)

    >
    > Duh, I thought it was one big ViewState shared between all controls on the
    > page!!
    >
    > Ho hum, I've just spent ages playing with ClientID to get this working. Oh
    > well, I might leave it now, if it ain't broke, don't fix it ;-)
    >
    > Thanks for the elucidation
    >
    > --
    > Alan Silver
    > (anything added below this line is nothing to do with me)
    Kevin Spencer, Feb 24, 2005
    #6
  7. Alan Silver

    Alan Silver Guest

    >I thought that would make you feel better. :)

    Oh it does, but it also makes me feel a bit daft for having spent so
    long trying to figure out how to avoid a non-existent problem!!

    Ta ra

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
    Alan Silver, Feb 24, 2005
    #7
    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. Replies:
    2
    Views:
    610
    Curt_C [MVP]
    Jul 27, 2004
  2. bazzer
    Replies:
    0
    Views:
    525
    bazzer
    Apr 10, 2006
  3. bazzer
    Replies:
    8
    Views:
    1,584
    kruch
    Mar 23, 2007
  4. Tim_Mac
    Replies:
    3
    Views:
    1,894
    Tim_Mac
    Sep 12, 2006
  5. Veli-Pekka Tätilä
    Replies:
    6
    Views:
    98
    Anno Siegel
    Aug 23, 2005
Loading...

Share This Page