Yow to Generate Unique Control ID?

J

Jeremy

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

Hans Kesting

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
 
P

Peter Bucher [MVP]

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.
 

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,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top