.NET Tutorials, Forums, Interview Questions And Answers
HomeTutorialArticlesForumInterview QuestionCode SnippetsTechnology NewsFun Zone Poll Certification Search
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share

 Subscribe to Articles

Google Search API implementation in asp.net Tutorial

Posted By :Nikhil Kumar      Posted Date :01/02/2010   Points :25   Category: ASP.NET    URL: http://www.dotnetask.blog.co.in

Google Search API implementation in asp.net. Here is an article to show how to implement or use 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>


Interface (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


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

Featured Articles


Best Practices No 5: - Detecting .NET application memory leaks
Memory leaks in .NET application have always being programmer's nightmare. Memory leaks are biggest problems when it comes to production servers. Productions servers normally need to run with least down time. Memory leaks grow slowly and after sometime they bring down the server by consuming huge chunks of memory. Maximum time people reboot the system, make it work temporarily and send a sorry note to the customer for the downtime. ... Read More
.NET Best Practice No: 1:- Detecting High Memory consuming functions in .NET code
One of the important factors for performance degradation in .NET code is memory consumption. Many developers just concentrate on execution time to determine performance bottle necks in a .NET application. Only measuring execution time does not clearly give idea of where the performance issue resides. Ok, said and done one of the biggest task is to understand which function, assembly or class has consumed how much memory. In this tutorial we will see how we can find which functions consume how much memory. This article discusses the best practices involved using CLR profiler for studying memory allocation.... Read More
How to improve your LINQ query performance by 5 X times ?
LINQ has been criticized by many early adopters for its performance issues. Well if you are just going to drag and drop using DBML code generator I am sure you will land up in to mess. Try doing this make a simple LINQ to SQL project using DBML and see your SQL profiler, I am sure you will never like to touch DBML code generator again. ... Read More
Responses
No response found. Be the first to respond this post
Post Comment
You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend