So I was looking for a way to 'randomly' display a picture from a given directory. I was going to get all crazy, and do a bunch of file system operations, and then some randomization on that. After I had started, I stopped. I thought this does not need to be so complicated!
I talked to a few friends, who thought my approach seemed fine. But to be quite honest I was in a lazy mood and decided that I didn't feel like comming up with some complicated solution for something that seems so easy. Then it hit me. Isn't there some AdRotator thing in .NET? Yeah there is, and it's slick.
I simply added the following code to my .aspx page.
<asp:AdRotator id="ar1" AdvertisementFile="headers.xml" BorderWidth="0" runat=server />
Well I'm not really using Ads. I have a header graphic similar to a banner ad, but I wanted it to be random each time the page loaded. The cool thing about using this control is that there is no codebehind logic to worry about.
There is then an xml file in this case called headers.xml with the followng structure:
<?xml version="1.0" encoding="utf-8" ?> <Advertisements><Ad><ImageUrl>images/header/header1_noglow.jpg</ImageUrl><AlternateText>Alt Text</AlternateText><Impressions>1</Impressions></Ad><Ad><ImageUrl>images/header/header_sky2.jpg</ImageUrl><AlternateText>Alt Text</AlternateText><Impressions>1</Impressions></Ad><Ad><ImageUrl>images/header/header_sky.jpg</ImageUrl><AlternateText>Alt Text</AlternateText><Impressions>1</Impressions></Ad><Ad><ImageUrl>images/header/header_window.jpg</ImageUrl><AlternateText>Alt Text</AlternateText><Impressions>1</Impressions></Ad></Advertisements>
So this is really basic stuff, but something I've always ignored in the past. So if you want a way to display a discrete set of images randomly, this might be the ticket.
Remember Me
Powered by: newtelligence dasBlog 1.9.6264.0
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2008, John Batdorf
E-mail