Blocks4.NET .net components for .net developers
Interested in getting reliable email hosting or server web hosting for your business? Check up out today!

Google AdSense for Search on ASP.NET Page

May 28, 2007 15:02 by blocks4

Several days ago, we have started to extend our search page with a Google Search Box. Very soon we faced  a problem with the code to add into our ASP.NET  2.0 page. The html code supplied by Google is contained into a couple of <form></form> tags. As only one couple of <form></form> tags can be present into a asp.net page and we already had  <form></form> tags in the page (from the master page we have), the result was that the search box was displayed properly but could not work.

AdSense implements the interaction with the website through a form on the page that posts a response directly to Google. Sample code:

       
<!-- Search Google -->
        <center>
            <form action="http://www.google.com/custom" method="get" target="_top">
                <table bgcolor="#ffffff">
                    <tr>
                        <td align="left" height="32" nowrap="nowrap" valign="top">
                            <a href="http://www.google.com/" mce_href="http://www.google.com/">
                                <img align="middle" alt="Google" border="0" src="http://www.google.com/logos/Logo_25wht.gif" mce_src="http://www.google.com/logos/Logo_25wht.gif" /></a>
                            <input maxlength="255" name="q" size="31" type="text" />
                            <input name="sa" type="submit" value="Search" />
                            <input name="client" type="hidden" value="pub-5037564150160211" />
                            <input name="forid" type="hidden" value="1" />
                            <input name="ie" type="hidden" value="ISO-8859-1" />
                            <input name="oe" type="hidden" value="ISO-8859-1" />
                            <input name="hl" type="hidden" value="en" />
                        </td>
                    </tr>
                </table>
            </form>
        </center>
        <!-- Search Google -->

After several tries and searches on the net we found a quick and dirty solution. Not the best, but the one that worked for us. We have modified the AdSense-supplied form to look like this:

    <!-- SiteSearch Google -->
                <script type="text/javascript">

                    function processSearch() {
                      var t = 'SearchResults.aspx?'

                      if (eval("document.aspnetForm.sitesearch[1].checked") == true)
                          t = t + 'sitesearch=www.blocks4.net';
                      else
                          t = t + 'sitesearch=web';
                     
                      t = t + '&q=' + document.aspnetForm.q.value;
                     
                      window.location = t;
                  }

                </script>

                <!--<form method="get" action="http://www.blocks4.net/SearchResults.aspx" target="_top" >-->
                <table border="0" bgcolor="#ffffff">
                <tr>
                   <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                <td nowrap="nowrap" valign="top" align="left" height="32">
                <a href="http://www.google.com/" mce_href="http://www.google.com/" class="snap_noshots">
                <img src="http://www.google.com/logos/Logo_25wht.gif" mce_src="http://www.google.com/logos/Logo_25wht.gif" border="0" alt="Google" align="middle"></img></a>
                </td>
                <td nowrap="nowrap">
                <input type="hidden" name="domains" value="www.blocks4.net"></input>
                <label for="sbi" style="display: none">Enter your search terms</label>
                <input type="text" name="q" size="100" maxlength="255" value="<%= strKey %>" id="sbi" class="searchBoxAdvanced" ></input>
                <label for="sbb" style="display: none">Submit search form</label>
                <input type="button" value="Search" onclick="processSearch()" class="searchButton" style="width:auto"></input>
                </td></tr>
                <tr>
                <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                <td>&nbsp;</td>
                <td nowrap="nowrap">
                <table>
                <tr>
                <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                <td>
                <input type="radio" name="sitesearch" value="" checked id="ss0"></input>
                <label for="ss0" title="Search the Web" class="txtAddNote">Web</label></td>
                <td>
                <input type="radio" name="sitesearch" value="www.blocks4.net"  id="Radio1"></input>
                <label for="ss1" title="Search www.blocks4.net" class="txtAddNote">www.blocks4.net</label></td>
                </tr>
                </table>
                <input type="hidden" name="client" value="pub-2224128234450239"></input>
                <input type="hidden" name="forid" value="1"></input>
                <input type="hidden" name="ie" value="ISO-8859-1"></input>
                <input type="hidden" name="oe" value="ISO-8859-1"></input>
                <input type="hidden" name="safe" value="active"></input>
                <input type="hidden" name="flav" value="0000"></input>
                <input type="hidden" name="sig" value="kEnDjVDLr5nAov8r"></input>
               <input type="hidden" name="hl" value="en"></input>
                </td></tr></table>
                <!--</form>-->
                <!-- SiteSearch Google -->

The idea is to replace the Submit button with a Javascript snippet that relays the user search text and site selection to a another ASP.NET page. The hidden fields that were originally part of the form are just ignored.

In the SearchResults.aspx page we have not inherited the master page (not a very good idea)  in order to avoid the <form></form> tags and used the original AdSense code. Then using dispatchRequest we call the original code and display the results using the second code provided by Google.

     <script type="text/javascript" language="javascript">
     function dispatchRequest() {
          
        document.google.q.value = '<%= Request.QueryString["q"].ToString() %>';
        if ('<%= Request.QueryString["sitesearch"].ToString()%>' == "www.blocks4.net")
            document.google.sitesearch[1].checked = true;
        else
            document.google.sitesearch[0].checked = true;
         if ('<%= Request.QueryString["domains"]%>' == "")
            document.google.submit();
    }
    </script>

...............

  <body onload="javascript:dispatchRequest()">

................

                           <!-- SiteSearch Google -->
                            <form method="get" action="http://www.blocks4.net/SearchResults.aspx" target="_top" name="google">
                            <table border="0" bgcolor="#ffffff">
                            <tr><td nowrap="nowrap" valign="top" align="left" >
                            <a href="http://www.google.com/" mce_href="http://www.google.com/">
                            <img src="http://www.google.com/logos/Logo_25wht.gif" mce_src="http://www.google.com/logos/Logo_25wht.gif" border="0" alt="Google" align="middle"></img></a>
                            </td>
                            <td nowrap="nowrap">
                            <input type="hidden" name="domains" value="www.blocks4.net"></input>
                            <label for="sbi" style="display: none">Enter your search terms</label>
                            <input type="text" name="q" size="31" maxlength="255" value="" id="sbi" class="searchBoxAdvanced" style="width:300px"></input>
                            <label for="sbb" style="display: none">Submit search form</label>
                            <input type="submit" name="sa" value="Search" id="sbb" class="searchButton" style="width:auto"></input>
                            </td></tr>
                            <tr>
                            <td>&nbsp;</td>
                            <td nowrap="nowrap">
                            <table>
                            <tr>
                            <td>
                            <input type="radio" name="sitesearch" value="" id="ss0"></input>
                            <label for="ss0" title="Search the Web" class="txtAddNote">Web</label></td>
                            <td>
                            <input type="radio" name="sitesearch" value="www.blocks4.net" id="ss1"></input>
                            <label for="ss1" title="Search www.blocks4.net" class="txtAddNote">www.blocks4.net</label></td>
                            </tr>
                            </table>
                            <input type="hidden" name="client" value="pub-2224128234450239"></input>
                            <input type="hidden" name="forid" value="1"></input>
                            <input type="hidden" name="ie" value="ISO-8859-1"></input>
                            <input type="hidden" name="oe" value="ISO-8859-1"></input>
                            <input type="hidden" name="safe" value="active"></input>
                            <input type="hidden" name="flav" value="0000"></input>
                            <input type="hidden" name="sig" value="kEnDjVDLr5nAov8r"></input>
                            <input type="hidden" name="hl" value="en"></input>
                            </td></tr></table>
                            </form>
                            <!-- SiteSearch Google -->


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Related posts

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag