Web developers wiki ASP.NET Sitecore Sharepoint Kentico by Evident Interactive

I’ve been using Google Custom Search Engine (CSE) for one of my projects (searchTravelBlog). When I was trying to implement CSE in NewGuid.NET I found some problems doing this.The big issue is that CSE depends on the html FORM element with submit event for posting all the variables from the hiddenfields.

The CSE code from Google looks like this:

<form action="http://" id="searchbox_004900934070880588383:m5qbf6oxe6k">
      <input type="hidden" name="cx" value="004900934070880588383:m5qbf6oxe6k" />
      <input type="hidden" name="cof" value="FORID:11" />
      <input type="text" name="q" size="25" />
      <input type="submit" name="sa" value="Search" />

When you copy this code into a .Net page it will not work. This because of the default .Net server form tag.

If you want to use CSE within your .Net page don't use Google's code that generates the search form. Create your own code, just add one Textbox and one Button control to your page.

<asp:TextBox ID="searchQuery" runat="server" size="30" />
<asp:Button runat="server" Text="Search .Net Info" ID="searchButton" OnClick="searchButton_Click" /> 

In the code behind you add the following searchButton_Click event:

protected void searchButton_Click(object sender, EventArgs e)
   StringBuilder redirectUrl = new StringBuilder();
   //The URL to your resultpage
   //Add your CSE unique identifier
   //Add your advertising location code
   //The search query
   redirectUrl.Append("&q=" + searchQuery.Text);
   //Redirect to the resultpage

You can find these values in the hidden values from the default code that Google creates for you.It is important that the values within the redirectUrl are html encoded.

Add the following code to your Page_PreRender so that the searchquery will appear in the textbox.

protected void Page_PreRender(object sender, EventArgs e)
   searchQuery.Text = Request.QueryString["q"];

Note: This example doesn't include the branding that Google requires. Please read the branding guidelines here!

Source: blog.newguid.net

 © Evident Interactive BV