Yow to Generate Unique Control ID?

  • Thread starter Thread starter Jeremy
  • Start date Start date
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.
 
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
 
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.
 
Back
Top