Questions about implementing AdSense search code

Topics: Themes
Jan 22, 2009 at 7:42 PM
I am using Visual Studio 2008 to view the web pages that I am creating.

I am trying to add Adsense search code to a text box widget in the side panel of the default theme.  However, after pasting all code and updating the widget it still remains unchanged.  I also notice that some of the code disappears when I click to view the HTML source.

I have pasted the code for the Search Box within the text box widget.  The code for the Search Results I have pasted into the main content area of a new page I created using the Pages control.  The URL I used to link the search box to the search results page follows this format:  http://www.mysitename/page/Search.aspx.

I suspect that the cause of the disappearing code and the unchanged text box are resulting from either the fact that the URL cannot be resolved this way or that I must first upload the blog to the my ISP host.  Part of the code that disappears is a line that specifies the URL.

Does anyone have any experience with this?  I would really appreciate some pointers...
Coordinator
Jan 23, 2009 at 4:40 AM
When you're putting the AdSense search code into the TextBox widget, are you just pasting it directly into the tinyMce WYSIWYG window, or are you clicking the HTML toolbar icon first so you can paste the AdSense search code in as HTML code?
Jan 23, 2009 at 3:23 PM
Edited Jan 23, 2009 at 6:37 PM
I am clicking the HTML toolbar icon first.

Still having the same problem.  Here is the website.  At top of the side panel above where it says "Related Links" is a small widget with no content.  This is where the search box is supposed to appear.

Scrolling down to the Page List, you will find a link to a page entitled "Search".  That is the page where I pasted the search results code.

Is there somewhere that I can paste in the html code within Visual Studio without using the website tools?  I don't know where to look for this little text box...

Coordinator
Jan 23, 2009 at 7:34 PM
It looks like the script is running, but I don't see anything visibly in that empty box above Related Links.  The <script> tag is there in the empty box:

<script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=en"></script>

And that script is injecting the JavaScript below into your page.  I'm not familiar with Google search, but it looks like the script is looking for elements on your page with an ID of either "cse-search-box" or "searchbox_demo".  Maybe you're supposed to create those elements ahead of time?

You can manually edit the TextBox contents, but if all you're trying to put in there is that <script> tag above, it looks like that part is already done.  The TextBox contents are stored under App_Data\datastore\widgets and it'll be one of the XML files.

(function() {
var f = document.getElementById('cse-search-box');
if (!f) {
f = document.getElementById('searchbox_demo');
}
if (f && f.q) {
var q = f.q;
var n = navigator;
var l = location;
if (n.platform == 'Win32') {
q.style.cssText = 'border: 1px solid #7e9db9; padding: 2px;';
}
var b = function() {
if (q.value == '') {
q.style.background = '#FFFFFF url(http:\x2F\x2Fwww.google.com\x2Fcoop\x2Fintl\x2Fen\x2Fimages\x2Fgoogle_custom_search_watermark.gif) left no-repeat';
}
};
var f = function() {
q.style.background = '#ffffff';
};
q.onfocus = f;
q.onblur = b;
if (!/[&?]q=[^&]/.test(l.search)) {
b();
}
}
})();
Jan 23, 2009 at 7:49 PM
Edited Jan 23, 2009 at 7:51 PM
Thanks, Ben.  I will have to edit the text box in Visual Studio because part of the code that I paste using the website tools disappears each time.  This is happening in both locations:  within the text box on the home page and in the Search.aspx page.  I suspect I will have to edit both of them in Visual Studio.

I haven't tried looking for the Search.aspx page yet.  I will work on this again tonight.
Jan 24, 2009 at 10:35 PM
While I don't see the code that you show in your post, I did find a Value field in a SerializableStringDictionary that seems to contain part of the code that I pasted into the text box.
<?xml version="1.0" encoding="utf-8"?>
<SerializableStringDictionary>
  <SerializableStringDictionary>
    <DictionaryEntry Key="content" Value="&lt;div&gt;&#xD;&#xA;&lt;/div&gt;&#xD;&#xA;&lt;script src=&quot;http://www.google.com/coop/cse/brand?form=cse-search-box&amp;amp;lang=en&quot; type=&quot;text/javascript&quot;&gt;&#xD;&#xA;&lt;/script&gt;&#xD;&#xA;" />
  </SerializableStringDictionary>
</SerializableStringDictionary>
Unfortunately, I really do not know how I should change this.  My only thought is to precede the string with the @ symbol and paste the Adsense Search Code.  Other than this, I have no idea.

Here is the search code from Google:
<form action="http://www.coalenergynow.com/page/Search.aspx" id="cse-search-box">
  <div>
    <input type="hidden" name="cx" value="partner-pub-5246581251760760:cvo83h-b3u0" />
    <input type="hidden" name="cof" value="FORID:11" />
    <input type="hidden" name="ie" value="ISO-8859-1" />
    <input type="text" name="q" size="31" />
    <input type="submit" name="sa" value="Search" />
  </div>
</form>
<script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=en"></script>
Also, do you know where I might find the code for the Search.aspx page that I created using the Pages control?  I will need to paste the search results code there.
Coordinator
Jan 25, 2009 at 12:13 AM
The "code" for the search.aspx page ... do you mean how do you get to the search.aspx page?  The url is:

http://www.coalenergynow.com/page/search.aspx

The JavaScript code I posted was the code that Google was injecting into your page at runtime.

The content that goes into the Value attribute of the DictionaryEntry tag needs to be HTML encoded.  When I paste that code into the BE 1.4.5 textbox widget editor, like you're experiencing, it's also getting truncated on me.  However, in the latest version of BE, which uses the new tinyMce edtior, it's not truncating the code I paste into the editor.  Below is the entire DictionaryEntry tag I get when pasting your code into the newer tinyMce editor.

    <DictionaryEntry Key="content" Value="&lt;form id=&quot;cse-search-box&quot; action=&quot;http://www.coalenergynow.com/page/Search.aspx&quot;&gt;&#xD;&#xA;&lt;div&gt;&lt;input name=&quot;cx&quot; type=&quot;hidden&quot; value=&quot;partner-pub-5246581251760760:cvo83h-b3u0&quot; /&gt; &lt;input name=&quot;cof&quot; type=&quot;hidden&quot; value=&quot;FORID:11&quot; /&gt; &lt;input name=&quot;ie&quot; type=&quot;hidden&quot; value=&quot;ISO-8859-1&quot; /&gt; &lt;input name=&quot;q&quot; size=&quot;31&quot; type=&quot;text&quot; /&gt; &lt;input name=&quot;sa&quot; type=&quot;submit&quot; value=&quot;Search&quot; /&gt;&lt;/div&gt;&#xD;&#xA;&lt;/form&gt;&#xD;&#xA;&lt;p&gt;&#xD;&#xA;&lt;script src=&quot;http://www.google.com/coop/cse/brand?form=cse-search-box&amp;amp;lang=en&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;&#xD;&#xA;&lt;/p&gt;" />
Jan 25, 2009 at 1:09 AM
Ok, so I just need to download the newest source code for BlogEngine.net, upload to the website, and paste into the textbox using the website tools?
Coordinator
Jan 25, 2009 at 1:20 AM
Yes, you can upgrade your BE installation to the latest version.  You do need to compile the BE core code yourself if you get the latest version off the Source Code tab above.  You can also just use that HTML encoded text I put in my last message if you don't want to upgrade right away.  .NET also can HTML encode anything with Server.HtmlEncode("text to encode").
Jan 25, 2009 at 1:43 AM
Is it detailed anywhere here about upgrading to the latest version using the source code?

Of course, I already know how to compile it thanks to your help previously.  However, my website already exists and I am not sure how to upgrade without endangering the work I have already done.

I know the obvious thing to do is to backup the website as it presently exists.  Then download the new source code and compile it.  Then I would simply FTP everything to the website choosing to only overwrite files if the source files are newer.

Is this all I need to do?
Coordinator
Jan 25, 2009 at 1:50 AM
Upgrading is often discussed in the Discussions here.  Here's one recent discussion from 2 days ago.

Yes, you'll want to overwrite your current files with the new files.  You won't want to overwrite your web.config file, of course.  I'd just let everything else overwrite.  And as long as you have a backup, there shouldn't be too much to worry about.
Coordinator
Jan 25, 2009 at 8:06 PM
Actually, I noticed the new version of tinyMce is basically preserving your AdSense code, but it does change the order of the tag attributes a bit and also adds <p> tags which aren't needed.  The following encoded HTML is a more direct encoding of the AdSense code:

&lt;form action=&quot;http://www.coalenergynow.com/page/Search.aspx&quot; id=&quot;cse-search-box&quot;&gt;
  &lt;div&gt;
    &lt;input type=&quot;hidden&quot; name=&quot;cx&quot; value=&quot;partner-pub-5246581251760760:cvo83h-b3u0&quot; /&gt;
    &lt;input type=&quot;hidden&quot; name=&quot;cof&quot; value=&quot;FORID:11&quot; /&gt;
    &lt;input type=&quot;hidden&quot; name=&quot;ie&quot; value=&quot;ISO-8859-1&quot; /&gt;
    &lt;input type=&quot;text&quot; name=&quot;q&quot; size=&quot;31&quot; /&gt;
    &lt;input type=&quot;submit&quot; name=&quot;sa&quot; value=&quot;Search&quot; /&gt;
  &lt;/div&gt;
&lt;/form&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;http://www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=en&quot;&gt;&lt;/script&gt;
Jan 26, 2009 at 4:55 PM
Ok, thanks again.

I have finally gotten the search box implemented.

However, it is neither redirecting to the Search page nor returning any results.  The code that should be on the Search.aspx page is this:

<div id="cse-search-results"></div>
<script type="text/javascript">
  var googleSearchIframeName = "cse-search-results";
  var googleSearchFormName = "cse-search-box";
  var googleSearchFrameWidth = 800;
  var googleSearchDomain = "www.google.com";
  var googleSearchPath = "/cse";
</script>
<script type="text/javascript" src="http://www.google.com/afsonline/show_afs_search.js"></script>
I believe that everything is there except for the very first line.  I am not sure if this is causing the problem or not.
Coordinator
Jan 26, 2009 at 11:40 PM
The Html I see when doing a View Source on your homepage is:

<div class="content"><form id="cse-search-box" action="http://www.coalenergynow.com/page/Search.aspx">
<div><input name="cx" type="hidden" value="partner-pub-5246581251760760:cvo83h-b3u0" /> <input name="cof" type="hidden" value="FORID:11" /> <input name="ie" type="hidden" value="ISO-8859-1" /> <input name="q" size="31" type="text" /> <input name="sa" type="submit" value="Search" /></div>
</form>
<p>
<script src="http://www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=en" type="text/javascript"></script>
</p></div>

I guess you put the <form> tag in there?  Strangely, even though that shows up in View Source, your <form> tag is disappearing.  I'm not sure if Google's JavaScript is removing the <form> tag, or ASP.NET is removing it.  I'd suspect it's ASP.NET because with ASP.NET, you're only allowed to have one form tag and ASP.NET already has its own form tag.  The remaining, default ASP.NET <form> tag has an action of "default.aspx" (your blog's homepage), and I can see that when clicking the Search button, it is indeed posting to default.aspx instead of search.aspx.

I'd search Google for terms like "asp.net nested form".  I think getting nested forms to work in ASP.NET requires a little bit of work.  Here's a couple of related pages I found in a quick search.

Nested Form in ASP.NET Page
http://www.cubiczone.com/Articles/tabid/65/EntryID/21/Default.aspx

Google Search and ASP.NET - would modifying AdSense code be permitted in this case?
http://www.google.com/support/forum/p/AdSense/thread?tid=2a9b892b35120b56&hl=en
Jan 27, 2009 at 3:12 AM
Edited Jan 27, 2009 at 3:15 AM
The first web page is unavailable.  From the second site I tried several different combinations as I attempted to implement the advice from "Step 1".  Unfortunately, I was not able to make any progress whatsoever.

I tried a Google search but I soon became exasperated with that.  I also searched on CodePlex and found a couple of AdSense widgets, but they did not seem to offer Search Box functionality.  Even if they did I am of the understanding that their usage would be against Google's AdSense policy.

Since my usage of the Google Search Box is against the AdSense policy in its present state and I can't get it to work correctly anyway, I believe I will just remove it from my website altogether. 

Thanks for all of your help on this one, Ben.  It is appreciated.