How to implement google search box in asp.net


In this post, I explain How to implement google search box in asp.net.

It's very difficult to create a search engine for a dynamic asp.net website. Google search engine is the best search engine. Here I have explained How to implement  google search box in asp.net.

Steps :

Step - 1: Create New Project.

Go to File > New > Project > Select asp.net web forms application > Entry Application Name > Click OK.

Step-2: Add a Webpage and Design for Show Google Search Result.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select web form/ web form using master page under Web > Enter page name > Add.

Step-3: Sign up & Setup your Search Engine for Google Search.

Go to https://www.google.com/cse/all > Sign up for Google Search > Setup your Search Engine for Google Search.

Step-4: Place Google Search Code to the Search Result Page (Where you want to show your result).

Open Search Result Page and place Google Search Code in your page where you want to show google search result.

<%-- Here I will Add code for show search result --%>
        (function () {
            var cx = '002672153316751420378:ho6psrycb1e';
            var gcse = document.createElement('script');
            gcse.type = 'text/javascript';
            gcse.async = true;
            gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
                '//www.google.com/cse/cse.js?cx=' + cx;
            var s = document.getElementsByTagName('script')[0];
            s.parentNode.insertBefore(gcse, s);


Step-5: Design & Write Code to your master page for Place Search Box & Implement Search.

Complete HTML Code for Master Page

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="ASPGoogleSearch.SiteMaster" %>

<!DOCTYPE html>
<html lang="en">
<head runat="server">
    <meta charset="utf-8" />
    <title><%: Page.Title %> - My ASP.NET Application</title>
    <link href="~/Content/Site.css" rel="stylesheet" /> 
    <link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <asp:PlaceHolder runat="server">        
        <script src="<%: ResolveUrl("~/Scripts/modernizr-2.5.3.js") %>"></script>
    <meta name="viewport" content="width=device-width" />
    <asp:ContentPlaceHolder runat="server" ID="HeadContent" />
        function SubmitOnEnter(searchBox, event) {
            var keyCode;
            if (window.event) {
                keyCode = window.event.keyCode;
            else if(event) {
                keyCode = event.which;
            else {
                return true;
            if (keyCode == 13) {
                // This is for Enter Key
                return false;
            else {
                return true;

        function SiteSearch() {
            document.location.href = "/SearchResult.aspx?q=" +
                EncodeText(document.getElementById('q').value); // Here we should use url encode for the user input

        function EncodeText(value) {
            var returnValue = "";
            var x = 0;
            var regex = /(^[a-zA-Z0-9_.]*)/
            while (x < value.toString().length) {
                var match = regex.exec(value.substr(x));
                if (match != null && match.length > 1 && match[1] != '') {
                    returnValue += match[1];
                    x += match[1].length;
                else {
                    if (value[x] == ' ') {
                        returnValue += '+';

                    else {
                        var charCode = value.charCodeAt(x);
                        var haxValue = charCode.toString(16);
                        returnValue += "%" + (haxValue.length < 2? '0':'') + haxValue.toUpperCase();
            return returnValue;
    <form runat="server">
    <asp:ScriptManager runat="server">
            <asp:ScriptReference Name="jquery" />
            <asp:ScriptReference Name="jquery.ui.combined" />
        <div class="content-wrapper">
            <div class="float-left">
                <p class="site-title"><a runat="server" href="~/">your logo here</a></p>
            <div class="float-right">
                <%-- Here I am going to add Our search control in the Master Page --%>
                <div style="width:300px; text-align:right; margin-bottom:10px;">
                    <input type="text" value="Search" style="width:200px;"
                        id="q" name="q" onblur="if(this.value == '') this.value=this.defaultValue;"
                        onfocus="if(this.value == this.defaultValue) this.value = '';"
                        onkeypress="return SubmitOnEnter(this, event);"  />                     
                    <input type="button" value="Search" onclick="SiteSearch();" style="padding:4px; margin:0px;" />
                <section id="login">
                    <asp:LoginView runat="server" ViewStateMode="Disabled">
                                <li><a id="registerLink" runat="server" href="~/Account/Register.aspx">Register</a></li>
                                <li><a id="loginLink" runat="server" href="~/Account/Login.aspx">Log in</a></li>
                                Hello, <a runat="server" class="username" href="~/Account/Manage.aspx" title="Manage your account">
                                    <asp:LoginName runat="server" CssClass="username" />
                                <asp:LoginStatus runat="server" LogoutAction="Redirect" LogoutText="Log off" LogoutPageUrl="~/" />
                    <ul id="menu">
                        <li><a runat="server" href="~/">Home</a></li>
                        <li><a runat="server" href="~/About.aspx">About</a></li>
                        <li><a runat="server" href="~/Contact.aspx">Contact</a></li>
    <div id="body">
        <asp:ContentPlaceHolder runat="server" ID="FeaturedContent" />
        <section class="content-wrapper main-content clear-fix">
            <asp:ContentPlaceHolder runat="server" ID="MainContent" />
        <div class="content-wrapper">
            <div class="float-left">
                <p>&copy; <%: DateTime.Now.Year %> - My ASP.NET Application</p>

Step-6: Run Application.

How to implement google search box in asp.net

Related Post :

  1. how to use google recaptcha in asp.net.
  2. How to create Google Combo chart with database data in ASP.NET
  3. How to create Google Column chart with database data in ASP.NET
  4. How to create google Bar chart with database data in ASP.NET
  5. How to create google line chart with database data in ASP.NET
  6. How to create google chart (pie) with database data in ASP.NET

Hello ! My name is Sourav Mondal. I am a software developer working in Microsoft .NET technologies since 2010.

I like to share my working experience, research and knowledge through my site.

I love developing applications in Microsoft Technologies including Asp.Net webforms, mvc, winforms, c#.net, sql server, entity framework, Ajax, Jquery, web api, web service and more.