Home » How To Guides | Technology | Useful tips

How can I add Google search box to my website on master page with asp.net?

26. May 2010 by Soan 2 Comments

You keep visiting sites and see that cool Google search boxes that let you either search your entire site or search the entire Google database, all without leaving your site's page! If you also want to have something like this, then I am sure you would have already got the Google Search Box for Adsense or have got the paid Google search box for your website.

The code works fine if your website has simple HTML or HTM pages. The moment you have master page (Which 99% of you would have in case your web site has been built using asp.net), the code provided by Google will throw an error if you place it on your master page.

What is the reason for Google search box error with Master Page in asp.net?


The master page by default contains a form tag and your google search box code also contains a form tag. The error is thrown because a single page cannot contain more than one form tag.

What is the solution to get rid of the error and show Google search box using master page?

  1. The solution is to create a new separate HTML/HTM page and place the Google Search box rendering code on this page instead of master page.
    This search box rendering code must be something like this:
    Google Adsense Search box rendering code
    If you want to copy the above code, you can download this file:Google Search Box HTML Page Code.txt (698.00 bytes)
    Generally, the above code will be provided to you by Google and you would not need to make any changes to it. In case you make some changes, just take care of following points:
    • Form Action= Should be your web site's search page created for showing the Google's search results. I will talk more about this step later in article.
    • The input tag with name="cx" should have the value of the partner code provided by Google.
  2. Now, to display search box on Master page, we should create an iFrame tag like this:
    Google Search box master page iFrame codeYou can download the above code from here: Google Search Box Master Page iFrame Code.txt (204.00 bytes)

  3. Create a new page (it can be any page on your web site) to show the search results and place the following code on your aspx file. This is the page which should be mentioned This code will also be provided to you by Google and you would not be required any changes to it.
    Google Search Box Search Result Page Code
    You can also download the code from here:  Google Search Box Search Result Page Code.txt (398.00 bytes)

    Thats it. You are done!. Just run your website and the google search box will be shown on your master page (And offcourse on all pages which are inherited from this master page). When you write something in search box and click search, it will open the search results in your search page (From step 3 above).

    Also, the multiple form error will also be solved using the above method.

    Google Search Box

    ENJOY!!

Comments

Jables
United States Jables said:

Thanks for sharing this method! I've been looking for a solution for implementing CSE on my ASP.NET site. The only problem I'm running into is when trying to use the back button in a browser, the page will just reload rather than going back. Do you know of any way to fix this problem?

Soan
United States Soan said:

Hi,
Share your code here so that i can find out the problem. I just tried the above solution and it works perfectly fine even if i click the 'back' button of browser.

Add comment


(Will show your Gravatar icon)

  Country flag


  • Comment
  • Preview
Loading