Search code examples
asp.netwebformsgoogle-search

Make Google Searchbox code compatible with ASP.NET WebForms


I was provided the following code to integrate into my ASP.NET WebForms page:

<form action="http://www.google.com/cse" id="cse-search-box" target="_blank">
  <div>
    <input type="hidden" name="cx" value="partner-pub-8127518365728966:9snx3s9v6fx" />
    <input type="hidden" name="ie" value="ISO-8859-1" />
    <input type="text" name="q" size="25" /><br />
    <input type="submit" name="sa" value="Search" class="formoutput"/>
  </div>
</form>
<script type="text/javascript" src="http://www.google.com/cse/brand?form=cse-search-box&amp;lang=en"></script>

However, I'm not sure how to do this because of the extra form element that this poses. Has somebody translated this to work with ASP.NET WebForms previously, and if so can you help me out?

Thanks!


Solution

  • Take a look here: http://am22tech.com/s/22/Blogs/post/2010/05/26/How-can-I-add-Google-search-box-to-my-website-on-master-page-with-aspnet.aspx

    It's a bit of a hack but the idea is to do the following:

    1. Place the search box in a separate HTML page
    2. Modify the Target and Action properties of the form
    3. Add an iFrame to your main page with the source of the above HTML page

    The article is pretty detailed and, if you don't mind using an iFrame, then it should answer your question.

    Update

    Scott Mitchell has posted a very detailed article that does not use iFrames. It looks like more work to implement but may be the better choice.

    URL: http://dotnetslackers.com/articles/aspnet/Implementing-Search-in-ASP-NET-with-Google-Custom-Search.aspx