InstantASP Support

Help & Support

Add Google AdSense to topics within InstantForum

Common Questions
This article details the steps necessary to automatically add Google AdSense ads within InstantForum topics. Although you can easily display Google AdSense through the wrapper functionality without any code changes adding your AdSense advertisements in-line within posts will increase your AdSense visibility and hopefully click-throughs.

You could easily extend this example to append the Google AdSense code to each post or use the topic keywords to determine the ads displayed. We've kept things simple in this example and are showing ads based on our AdSense ClientID. We do intend to improve support in this area with future versions to provide several methods for serving your ads with no code changes.

Add Google AdSense Ads Inline

Using the code in this example will allow you to add AdSense Ads automatically as the second post within any topic within InstantForum. This is shown below...

Code Changes (Post.vb)

This code can be applied to either the End User License or Developer License. You'll only need to modify a single code-behind file.

Open up InstantASP.InstantForum.UI\Pages\Post.vb within Visual Studio.NET

Locate the "InsertPost" method around line 660. We'll need to add a call to our new methods here.

Below the InsertPost method add the following code. You can also find this code in the attached file below.

InsertAdSensePost - This method actually adds the post to the InstantForumdatabase.

private Int32 InsertAdSensePost(Int32 intTopicID)

// get adsense post
InstantASP.InstantForum.Components.Topic AdSensePost = GetGoogleAdSensePost(intTopicID);

// add the post as a reply to our topic
InstantASP.InstantForum.Components.Topic Topic = InstantASP.InstantForum.Business.Posts.InsertPost((Int32)ForumID.Value, AdSensePost.TopicID, AdSensePost.ParentID, AdSensePost.UserID, ctlPostControl.MessageIcon, ctlPostControl.Subject, "", AdSensePost.Message, "", false,

if ((Topic != null)) {
return Topic.PostID;
} else {
return 0;


GetGoogleAdSensePost - This method simply returns a InstantASP.InstantForum.Components.Topic object representing the Google AdSense post we wish to add/ You should modify the ClientID and AdSense client side code below to suite your requirements.

private InstantASP.InstantForum.Components.Topic GetGoogleAdSensePost(Int32 intTopicID)

// IMPORTANT: Your AdSense Client ID
string strAdSenseClientID = "pub-XXXXXXXXXXXXXX";

// ----------
// The actual Adsense Code that will be added to our post
// You can modify this to suite your requirements
// ----------
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script type=\"text/javascript\"> ");
sb.Append("google_ad_client = \"" + strAdSenseClientID + "\"; ");
sb.Append("google_alternate_color = \"D9DAD5\"; ");
sb.Append("google_ad_width = 468; google_ad_height = 60; ");
sb.Append("google_ad_format = \"234x60_as\"; ");
sb.Append("google_ad_type = \"text\"; ");
sb.Append("google_ad_channel =\"\"; ");
sb.Append("google_color_border = \"676B56\"; ");
sb.Append("google_color_bg = \"D9DAD5\"; ");
sb.Append("google_color_link = \"800000\"; ");
sb.Append("google_color_url = \"800000\"; ");
sb.Append("goggle_color_text = \"333333\"; ");
sb.Append("<script type=\"text/javascript\" src=\"\"></script></div>");

// create an instance of our anonymous user, you'll use this user for the adsense post
InstantASP.InstantForum.Components.AnonymousUser User = new InstantASP.InstantForum.Components.AnonymousUser();

InstantASP.InstantForum.Components.Topic AdSensePost = new InstantASP.InstantForum.Components.Topic();
AdSensePost.TopicID = intTopicID;
AdSensePost.ParentID = intTopicID;
AdSensePost.UserID = User.UserID;
AdSensePost.Username = User.Username;
AdSensePost.Message = sb.ToString();

// return our topic object
return AdSensePost;


Once you've added the two methods above simply add a call to InsertAdSensePost from our existing InsertPost method...

private Int32 InsertPost()

// add post to database, we indicate this is not a poll below
// if successfull a topic object is returned with new post
InstantASP.InstantForum.Components.Topic Topic = InstantASP.InstantForum.Business.Posts.InsertPost((Int32)ForumID.Value, (Int32)TopicID.Value, (Int32)ParentID.Value, base.CurrentContext.CurrentUser.UserID, ctlPostControl.MessageIcon, ctlPostControl.Subject, ctlPostControl.Description, ctlPostControl.Message, ctlPostControl.AttachmentGUID, false,

// was post insert ok

if ((Topic != null)) {
// do we have a post

if (Topic.PostID > 0) {
// call our method to add a adsense post

// we've removed the rest of the code for readability.




Rebuild your solution within Visual Studio and visit your forum installation. Add a new test topic and you should see your Google AdSense ad displayed below the first post in your topic.

That's It!

Making these changes to Post.vb will ensure a Google AdSense ad always appears as the second post within any topic within InstantForum. I hope this helps generate a little extra income from your community.

If you have any questions don't hesitate to leave some comments below or contact us.

Attachments 1.00 KB, 2.5K views

Related Links

Optionally provide private feedback to help us improve this article...

Thank you for your feedback!

Comments require login or registration.


Product: InstantForum
Version: All Versions
Level: Beginner
Article not rated yet.
Article has been viewed 193K times.
Last Modified: 4 Years Ago
Last Modified By: Ryan Healey


Similar Articles