Google API in ASP.NET

Google API in ASP.NET

Here is an article to show how to implement or use Google API in Asp.NET.

Background

Using Google API means you are using google searching in your website

First Step:-

For using Google API you should have a Google Account for this just simply create a Gmail Account which is free of cost.

Then you have to get the Google API key this is also free, simply search on google for Google API key.

Secong Step:-

Now create an ASP.Net website and add this code to Defaul.aspx page

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Google API:</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <strong><span style="font-size: 16pt; color: #ff0000; font-family: Courier New CE">

      Google API In ASP.NET </span></strong>

            <br />

            <br />

        <hr style="font-weight: bold; color: #006600; height: 5px"/>

      <asp:Label ID="lblmsg" runat="server" Text=""></asp:Label>

        <br />

            <br />

       <asp:TextBox ID="txtsearch" runat="server" BackColor="#FFFFC0" BorderColor="Black" Width="321px" Height="18px" ></asp:TextBox>

       <asp:Button ID="btn1" runat="server" Text="Search" BackColor="#FFFFC0" BorderColor="#80FF80" ForeColor="#004000" OnClick="btn1_Click" Width="95px" />

        <br />

        <br />

        <hr style="font-weight: bold; color: #ff3333; height: 1px; text-decoration: line-through" />

    </div>

    </form>

</body>

</html>

 

InterfaceL (Defaul.aspx)

aaaaaaaaaaaaaaaa.JPG

 

Default.aspx.cs

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    protected void btn1_Click(object sender, EventArgs e)

    {

        if (txtsearch.Text.Trim().Length == 0)

        {

            lblmsg.Text = "Please enter your query to search...";

        }

        else

        {

            Response.Redirect("SearchDetails.aspx?q="+txtsearch.Text);

        }

    }

}

 

Third Step:-

Add a new .aspx page and give the name “SearchResult.aspx”.

Fourth Step:-

Add this code to

SearchResult.aspx:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SearchDetails.aspx.cs" Inherits="SearchDetails" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title>Google Search...</title>

    <script src="http://www.google.com/jsapi?key=Add your key here" type="text/javascript"></script>

 

    <script language="Javascript" type="text/javascript">

        //<![CDATA[

        google.load('search', '1');

 

        function OnLoad() {

 

           

            var searchControl = new google.search.SearchControl();

 

            // Add in a full set of searchers

            var localSearch = new google.search.LocalSearch();

            searchControl.addSearcher(localSearch);

            searchControl.addSearcher(new google.search.WebSearch());

            searchControl.addSearcher(new google.search.VideoSearch());

            searchControl.addSearcher(new google.search.BlogSearch());

            searchControl.addSearcher(new google.search.NewsSearch());

            searchControl.addSearcher(new google.search.ImageSearch());

            searchControl.addSearcher(new google.search.BookSearch());

            searchControl.addSearcher(new google.search.PatentSearch());

 

            searchControl.draw(document.getElementById("myCtrl"));

 

            // execute an inital search

            searchControl.execute(GetQueryString("q"));

        }

        google.setOnLoadCallback(OnLoad);

 

        //]]>

        // Function to get Query String Value

        function GetQueryString(query) {

            hu = window.location.search.substring(1);

            gy = hu.split("&");

            for (i = 0; i < gy.length; i++) {

                ft = gy[i].split("=");

                if (ft[0] == query) {

                    return ft[1];

                }

            }

        }

 

    </script> 

 

</head>

<body>

    <form id="form1" runat="server">

    <div id="myCtrl" style="width:100%">

   

    </div>

    </form>

</body>

</html>

 

NOTE: Add you key here = Copy your key from Google and paste in place of “add your key here

If you feel any problem in getting Google API key, then contact me I will be happy to help you J

OutPut:-

When you enter some query in textbox and click the search button it will redirect this page to SearchResult.aspx page where we are using script provided by Google.

Note: When you are getting Google API Key then you have to enter the website name, there you have enter http://www.google.com.

Conclusion:-

Here we learn how to use Google API in ASP.NET.

Thanks !!!

Nikhil Kumar

 

Ebook Download
View all
Learn
View all