Jonathan Wood said:
1. I know how many ads are on a page; however, control instances do not
know how many other instances there are.
2+3. Something like that.
I'm not sure how that would work. Just to clarify, I don't know if I used
the term "a control to monitor other controls" but all I really mean is a
separate control to coordinate things. Exactly how that is accomplished is
what I'm asking.
The ads are coming from my database so really there's no no client-script
involved.
For me the, issues are A) I suspect it is less efficient to have multiple
controls hit the database than to hit the database once for all controls
on a page, and B) when separate controls hit the database separately, I
can get duplicate ads on different controls.
Since everything is coming from the database, grab the list of ads and make
sure they are filtered to the number you want on the page. Then dynamically
instantiate the proper number of controls.
If this does not work, I would suggest iterating through the controls and
finding all ad controls. If you truly have an "ad" control, it will be easy.
If it is merely an image link type of object, you will need naming
convention on top to ensure you only grab ad images. If you want a count
before grabbing ads, you can either
a) iterate back through when you are adding ads (since the number is low,
iterating is fine)
b) store in a collection and then iterate through the collection (this is
only a benefit if the number of ads on a single page is ridiculous or there
are huge numbers of images, as the cost of instantiating the collection will
be heavier than iteration through images again). It is conceptually cleaner,
however, to go through only once, so it is not a bad pattern, but understand
you may negatively impact performance (in the milliseconds range) for the
added "beauty" in the code.
Once you know your objects and ads, just start feeding. If you are using
postbacks, you can update on a postback. If you want to use AJAX to reset
the list, you can do that as well, although you have to think through the
problem a bit more.
If you need to rotate the ads, set up the JavaScript to swap out images and
HREFS from a list or lists before sending to the client. This is a bit more
complex, but there are libraries out there that can help you if you google
for JavaScript image libraries. I tend to look at JQuery first, but I am not
sure if it has any functionality for anything like this. My reason is
twofold:
1. I use it a lot and it has a lot of bang for the buck
2. Microsoft is putting full support for JQuery in the 4.0 timeframe.
--
Peace and Grace,
Greg
Twitter: @gbworld
Blog:
http://gregorybeamer.spaces.live.com
************************************************
| Think outside the box! |
************************************************