Hi, I've added an AdRotator to my Home page by editing Skin-Ads.ascx and Ads.Xml
These Ads are currently displaying on all pages of my CS.
Is it possible to have AdRotators displaying specific ads on specific pages.(It's all 'custom ads' not Google Ads)
I would for example have certain advertisers who would like their add only to display on the forum default page or only at the top of a specific forum;
Anyone with ideas on how this can be done?
Hey
I'm reckoning that if you've added an AdRotator to the page you know a bit about code...
I guess that you could write some code that fires when the control is initialized to dynamically assign varying 'ads.xml' files according to the page's url.
Thus, if the page's url is 'x' then assign the AdRotator to use 'Ads.Group1.xml'Else if the page's url is 'y' then assign the AdRotator to use 'Ads.Group2.xml'
If you don't want a page to show adds at all, or if you want a default group of adds to display it's easy to incorporate that into your logic. So go through the list of pages you want to customize, set the xml accordingly or tell it to disable the AdRotator. If it gets through this list then you don't want customization of ads on the current page so use your default ad group.
Does that make sense? Is it helpful?
Paul.
Thanks Paul
I understand what you are saying, but I am not sure how one would say "If current Page = X then..."
Any idea on how to do this?
I thought it might be possible to edit the specifc default pages (like forum or blog default) and add the adds to that page....that would however not solve getting specific add to dislpay on specific forums;
Maybe there is a way of doing this by using AdParts?
Thanks for your help;
Hi again
I actually implemented something like this for a client the day after I left my last on this thread...!
Okay - First of all I'm a VB guy so considering there's no code written in c# in the control it's okay to change it to VB. Also I'm a believer in code-behind and it's quicker and easier to use a code-behind file in which to write the code. The easiest way to do this is to create a NEW control (with a different name - say 'Skin-Ads2.ascx') and copy and paste the mark-up from the old .ascx file to the new one (that's everything appearing after the <%@ Control Language="C#"> declaration. Now simply delete the existing Skin-Ads.ascx and rename Skin-Ads2.ascx to Skin-Ads.ascx. We'll also want to be able to turn off the <div> element with the rotator in, so let's set it to run at the server and give it an id.
You're Skin-Ads.ascx file shoud look like this:
<%
<div id="AdLayer" align="center" style="padding-top:5px;padding-bottom:5px;" runat="server"> <asp:AdRotator ID="AdRotator1" runat="server" /></div></span><span id="Square" runat="server" visible="false"></span><span id="Skyscraper" runat="server" visible="false"></span><span id="StandardBottom" runat="server" visible="false"></span><span id="GoogleAdsense" runat="server" visible="false"></span><span id="Inline" runat="server" visible="false"></span>
Now you need to go to the code-behind file and add a proc for the Page_Init event in which to put your logic. Here's how I would implement your needs:
Partial
That should just about do it.
Just post back if you have any queries.
Paul,
Thanks for posting this. Apologies for it not showing up right away. It got tagged for moderation, but I released it. :-)
Regards,Dave
Thank you so much;
And thank you for all the comments to the code....I really appreciate it;
Great work!
Paul, the code works great, thank you again;
Can you just help me with one part of it?
I added this to find the last part of the current page's url:
x = Len(CurrentPage)
CurrentPage = Microsoft.VisualBasic.Right(CurrentPage, x - 30)
My path to CS has 30 characters
This works just fine for pages like "forums/default.aspx"
But as soon as I try a specific forum or thread like..."forums/16/ShowForum.aspx"....it doesn't work;
I did also add it to...
With CurrentPage2AdGroups
.Add("forums/16/ShowForum.aspx", SpecificAds)
End With
I must be missing something silly, or would you recommend using a different method to determine the
current page?
Regards
Andre
Okay.
First of all (and I didn't make this clear in my code) I was explicity importing the current page's url into lowercase so as to avoid having to match against case as well as, well, path. I suspect that's all the problem is. So when you create your individual pages in the "CurrentPage2AdGroups" put them all in lowercase.
Better still put the following after the "With CurrentPage2AdGroups... End With" block:
dim KVP as Generic.KeyValuePairfor each KVP in CurrentPage2AdGroups KVP.value = KVP.value.ToLowernext
And also after the "With NoAddPages.... End With" add:
dim NoAddPage as stringfor each NoAddPage in NoAddPages NoAddPage = NoAddPage.ToLowernext
This will ensure that all your strings are converted to lowercase for your comparisons.
Basically make sure all your comparisons are against lowercase strings. And a caveat - I created the above code by hand directly in the browser - I think it's fine but I didn't fire up Visual Studio to write it so it may not be perfect - but you get the gist right?
Also maybe an easier way of removing the stuff you don't want in the page's url, assuming that the url is http://www.mydomain.com/cs/forums/default.aspx:
Dim CurrentPage As String = Page.Request.Url.ToString.ToLower.Replace("http://www.mydomain.com/cs/", vbNullString)
Don't forget we want to remove the "/" before "forums/default.aspx".
Tell me how you got on.
Paul, it makes 100% sense;
So I changed the page in "CurrnetPage2AdGroups" to all lowercase...but it still doesn't work;
"forums/11/showforum.aspx"
I also tried it by changing the code to your suggestion on removing the stuff from the url...still no luck;
forums/default still works fine
Any idea what I am doing wrong?
Hmmm.... Strange it works sometimes and not others.
Do you have Visual Studio? If you did then you could set a breakpoint on the bits that compare the pages against our selection criteria and see what's happening. You can download a free version (Visual Studio Express Web Developer) from Microsoft.
Try it and see. If it doesn't work then send me back a copy of the files and I'll see try to see what's going on. Not quite sure how much time I have though as I've an awful lot on. But I'll try .
Paul thank you for taking a look at those files and ALL your help...it works great now;
I'll post the code (as you requested me to do) and the rest of this message for anyone else interested;
It turned out that the if you go to an specific forum the browser will display something like... http://www.mydomain/forums/11/ShowForum.aspx the but internally the url is changed to http://www.mydomain/forums/showforum.aspx?forumid=11
So Paul created a label, which can be disabled, to display the actual internal url;
This is the code for the Skin-Ads.ascx.vb
Label1.Text = CurrentPage
ShowPageLayer.Disabled =
.Add(
KVP.Value.ToLower()
SetAdRotatorFile(BlogAds)
NoAddPage = NoAddPage.ToLower
DisableAdRotator()
SetAdRotatorFile(DefaultAds)
AdRotator1.Enabled =
AdLayer.Visible =
AdLayer.Style.Clear()
AdRotator1.AdvertisementFile = location
End
Also add the label to Skin-Ads.ascx
<
</
It's a great functionality to have and I hope others find this usefull;
Thank you Paul!
Why go through the trouble of lowercasing and comparing/replacing/etc... Isn't that what regular expressions are for?Page.Request.Url.ToString.ToLower.Replace("http://www.mydomain.com/", vbNullString)
could be done...RegEx.Replace(Page.Request.Url.ToString(), "https?://[^/\\]*\.?mydomain\.com/?[\s$]", string.Empty, RegexOptions.IgnoreCase And RegexOptions.SingleLine) Ya... I'm not a big fan of vbNullString either... try string.Empty instead...