Mike Ormond's Blog

Musings on mobile development and Windows Phone 7 in particular.

Adverts and AsyncPostbacks

Adverts and AsyncPostbacks

  • Comments 2

Someone asked me in Glasgow how they could take advantage of ASP.NET AJAX on their site but still maintain their advertising banners which refresh on postback. Fewer postbacks = fewer refreshes = fewer payments (I was surprised to learn that revenue was generated per view rather than per click). The advertising company typically provides a line of script something like:

<script src="http://mikeo.co.uk/adverts.ashx" type="text/javascript"></script>

which then injects some JavaScript into my page which dynamically creates the markup for my advert, eg

document.write('<img src="http://mikeo.co.uk/randomadvert.jpg" alt="Advert" />');

And in the case in question, you passed a random number to the script and it would return an advert to you (something a little more complex that just an image but you get the idea). Problem is you can't just wrap this in an UpdatePanel and expect it to work as "rendering script or markup directly...such as by calling the Write(string) method" is not supported. Some other technique is required.

Well I wrestled with a few but all of them failed due to cross-domain security restrictions on the browser as the script I'm trying to download comes from a domain owned by the advertising company. More head scratching...

The only way I could see to requery the advertising company script was to create a web service that I could easily call from client script using the features of ASP.NET AJAX (ie add a service reference to my ScriptManager and ASP.NET AJAX takes care of building the JavaScript proxy for me and doing the serialisation/deserialisation so I can call the web service just as if it was an asynchronous JavaScript method on my page). All the web service has to do is execute a WebRequest against the advertising company URL, get the response stream and strip out the "document.write('" and the closing parentheses.

I now have a string representing the markup for the advert and I can return this as a string to the client. On the client side, I take the result (the markup string) and simply set the innerHTML of a suitable <div> tag to host the advert. And voila - it works. There are a few problems however:

  • The advertising company now sees all requests for my customers as coming from my server. As they're paying per view, I'd assume they have some sort of anti-fraud mechanism to prevent some unscrupulous person just pinging the script repeatedly. That's not exactly how we're going to appear to them...
  • The mechanism is less efficient as there are extra web requests involved
  • The mechanism puts a greater load on my servers as each page request now creates an extra web service call (this may be offset by some of the AJAX benefits though)
  • Stripping the response to arrive at the markup is a bit hacky
  • And probably some others you'll now point out to me... :-)


Technorati tags: , ,
  • Have you tried with

    ui.ScriptManager.RegisterClientScriptInclude(page, page.GetType, "ads", "http://officedinosaur.com/adverts.ashx")

  • Hmm. I've tried various permutations and combinations but nothing that works along these lines. The difficulty seems to revolve around re-executing a piece of script that loads another script using a <script> tag and the src attribute. If anyone gets this to work I'd be very interested...

Page 1 of 1 (2 items)