Thomas’ Developer Blog

August 5, 2008

Adsense meets AJAX (One Line Solution)

Filed under: AJAX, javascript, — Tags: , , , , — sanzon @ 3:40 am

I’m sure if you use AJAX and adsense with .Net you have run into a situation like this:

<asp:updatepanel id=”updatepanel1″>
  <!– ADSENSE Code –>

Doesn’t seem that big of a deal, right? Until you update that panel and the adense goes away! Why is this? adsense requires the code to read whatever is on the page before it is implemented. On top of that adsense requires inbound javascript, which you can’t really do that easily with scriptmanager, and it becomes a pain in the but to do it that way.

So how do you fix this?

It requires two divs, and that’s pretty much it!

<div id=”adsense_container” style=”display:none;”>

<asp:updatepanel id=”updatepanel1″>
  <div id=”adsense_unit”></div>

As you see you have two new divs and adsense is now outside the updatepanel. So everytime you update the panel, adsense is left out. Making it free to access anytime. Yet the adsense ad can’t be seen either.. what’s the point of this? How about this for an idea, why not take the contents of the adsense container and place them inside the adsense unit? With our little friend scriptmanager.

In your server side code type:

ScriptManager.RegisterStartupScript(Page, Page.GetType(),
“ScriptManager_SetAdsense”, “$get(‘adsense_unit’).innerHTML=
$get(‘adsense_container’).innerHTML”, True)

See how simple? lol Took only 1 line of code. Just placing one objects innerHTML inside another each time you access that updatepanel. Now! Remember, this only works if you have a way to access the updatepanel, aka a button.

In my case I had several tabs with AJAX content, which was no biggy since I just have to copy the code over each time.

Now is this against google’s TOS? I’m not sure yet, but I highly doubt it is since I’m not editing code, only replacing it. Which is no different then having a page reload each time, increasing impressions. I do have a post on the adsense group to see what they say. I’ll let you know what happens in a comment to this post. It should be fine as far as I know though.

Hope this helps!



  1. Hi,
    I have a problem that can’t be fixed with your solution, aparently. What about if the google adsese code has a dynamic parameter that changes on each partial postback? How can i change the adsense results then?
    I’d appreciate any help.

    Thank you!

    Comment by bogdan — March 2, 2009 @ 10:43 am

  2. I am facing a similar issue – I have the UpdatePanel in the MasterPage and the AdSense are placed in the child pages. On every partial postback, the AdSense are lost.

    I would be thankful if you can give some solution.


    Comment by Mohan — April 21, 2009 @ 3:32 am

  3. Hi,
    The problem was solved by me only with an iframe. Of course, the iframe contains a page only with the adsense and it’s an easy solution if the AdSense code takes parameters. But what to do if the code does not take parameters and instead uses the html context to establish the relevant results? Well, then you have to load a lot of hidden data in the iframe and delay everything.
    Anyway, i did not find any solution that does not imply using a iframe.

    Comment by bogdan — April 21, 2009 @ 6:09 am

  4. Hi guys,

    You will find the solution here:

    Simply start a ‘campaign’ and paste the code from Google Adsense™ in it.

    You can see the Advert-Rotator™ in action whitin an Ajax updatepanel on this site:

    The code is placed in an AJAX updatepanel and Google Adsense™ refreshes with every update of the panel. Its also rotates with other ads.
    The advert-rotating-tool is Adsense Safe™ !

    Be aware that when the content of the ajax-updatepanel has only minor changes the ads from Google could be the same. This can be solved by adding an other ad or advert tot the same ‘campaign’. In this way there will be an other advert on every update.

    Best regards,

    Comment by Bonaparte — July 22, 2009 @ 7:39 pm

  5. Hi – I had to change the single quote in the script to a double quote and then it functioned correctly as per below:

    ScriptManager.RegisterStartupScript(Page, Page.GetType(), “ScriptManager_SetAdsense”, “$get(” + Chr(34) + “adsense_unit” + Chr(34) + “).innerHTML=$get(” + Chr(34) + “adsense_container” + Chr(34) + “).innerHTML”, True)

    Comment by NoIdea — August 11, 2013 @ 10:52 am

  6. I have used the following code
    ScriptManager.RegisterStartupScript(Page, Page.GetType(), “ScriptManager_SetAdsense”, “$get(” + Chr(34) + “adsense_unit” + Chr(34) + “).innerHTML=$get(” + Chr(34) + “adsense_container” + Chr(34) + “).innerHTML”, True)
    I called this on pageIndex Changing of grid which is under update panel ,but still its causing the page refresh for me since i have used the update panel.

    Comment by Imran — February 12, 2014 @ 3:39 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at

%d bloggers like this: