How do I uniquely identify a control?

A

Alan Silver

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
 
K

Kevin Spencer

Every Control has its own ViewState. :)

--
HTH,

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

jhcorey

Alan said:
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

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
 
A

Alan Silver

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
 
A

Alan Silver

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

Kevin Spencer

I thought that would make you feel better. :)

--
HTH,

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

Alan Silver

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top