Only one instance of my control per Page

Z

Zarko Gajic

Hi, what's the best practice of ensuring that only one instance of a
(custom) control can be placed on a Web form (either at design or run time)?

~ Zarko
 
B

Brock Allen

Hmm, interesting requirement.

At runtime you could put a flag in the HttpContent.Items collection to indicate
that there is one on the page already. Perhaps you'd set this in the ctor
or in Init event for the control. This would work since the Items collection
is a per-request bag of data that is recreated for every request. This is
the first idea off the top of my head, but I don't know how much I like it....

What's the exact requirement? And why exactly is this a requirement? I'm
curious...
 
Z

Zarko Gajic

Brock,

My idea was to have a static field that would increment in the constructor
.... this way I could count the number of instances "from inside" my control.
I'm just not sure where to decrement the counter (destructor? does it get
called in the designer when you remove the control - haven't tried that
yet.)

I'm developing a simple IExtenderProvider for System.Web.UI.Page - there's
no point of having two on the page.

~zarko
 
A

Antonio Bakula

Brock,

My idea was to have a static field that would increment in the constructor
... this way I could count the number of instances "from inside" my control.
I'm just not sure where to decrement the counter (destructor? does it get
called in the designer when you remove the control - haven't tried that
yet.)

I'm developing a simple IExtenderProvider for System.Web.UI.Page - there's
no point of having two on the page.


maybe singleton pattern is what you wont ? I didn't work with singleton
controls, only singleton objects but maybe this is solution

http://www.yoda.arachsys.com/csharp/singleton.html
 
Z

Zarko Gajic

Hi, what's the best practice of ensuring that only one instance of a
(custom) control can be placed on a Web form (either at design or run
time)?

To all concerned:

Solved, using class static integer variable to count the number of
instances. The variable increments in constructor , decrement in destructor
(Dispose overridden) ... Render used to test for the number of instances.

~Zarko
 
B

Brock Allen

Well, the static would get incremeneted for two users hitting two different
pages at the same time, and so I don't think this would work for your requirements.
Also, there is no notion of deterministic finalization (like a dtor from
C++) in .NET. You might be able to handle the Unload event on your control,
but I suppose there are situations where that would not get called, so no
guarentees.

I still think the per-request Items collection is your best bet. This won't
help at design time, though.
 
B

Brock Allen

What happens when two different users are accessing that page at the same
time? The static variable is shared across all instances of your control.
Maybe you've already accounted for this...?
 
Z

Zarko Gajic

Brok,
I still think the per-request Items collection is your best bet. This won't
help at design time, though.

I've mixed the two approaches: at design time .. an instance counter is
used; at run time .. Context.Items is used (as you suggested).

p.s.
I've run a few test and this seems to work ...

~Zarko
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top