EaseSoft.NET EaseSearch 

Building your website Search Engine with EaseSearch

Download EaseSearch [ZIP 8kb]

Download EaseSearch c# Source Code

Set Up EaseSearch

1. Create a Google Account
To set up your own search engine, you must create a Google Account and obtain a license key. Your Google Account and license key entitle you to 1,000 automated queries per day.

2. Copy files
Creat a "bin" sub-folder within your web folder,copy EaseSearch.dll in "bin", copy EaseSearch.aspx in your web folder.

3.Set up your google license key and your domain name
Using your licese key and your domain name replace the following two lines in EaseSearch.aspx.

<asp:Label id=GoogleKey runat="server" Visible="False">licence key you got from google</asp:Label>
<asp:Label id=Domain runat="server" Visible="False">your domain </asp:Label></td>

4.Browse http://localhost/WebDir/EaseSearch.aspx. That's it!

 

Source Code Structure

Aadd web reference from http://api.google.com/GoogleSearch.wsdl and add this file googleproxy.cs,

create EaseSearch.aspx and EaseSearch.aspx.cs

EaseSearch.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using GoogleWebService;
namespace EaseSoft.Utitlity
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class EaseSearch: System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList DataList1;
protected System.Web.UI.WebControls.LinkButton PrevPage;
protected System.Web.UI.WebControls.DataList DataList2;
protected System.Web.UI.WebControls.LinkButton NextPage;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.TextBox SearWords;
protected System.Web.UI.WebControls.Button Search;
protected System.Web.UI.WebControls.Label Results;
protected System.Web.UI.HtmlControls.HtmlForm frmSiteSearch;
protected System.Web.UI.WebControls.Label GoogleKey;
protected System.Web.UI.WebControls.Label Domain;
int PageRows=10;

public GoogleSearchResult GoogleSearch(string keywords,int record)
{
string key=GoogleKey.Text;//"IBl1dkdQFHJa8VQknkvdWudugS4TY3o0";

GoogleSearchService s = new GoogleSearchService();
GoogleSearchResult r = s.doGoogleSearch(key, keywords,
record, PageRows, false, "", false, "", "", "");
return r;
}

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
this.SetCurrentRecord(0);
Results.Visible=false;
PrevPage.Visible=false;
NextPage.Visible=false;
// DisplayResult(0);
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SearWords.TextChanged += new System.EventHandler(this.SearWords_TextChanged);
this.Search.Click += new System.EventHandler(this.Search_Click);
this.PrevPage.Click += new System.EventHandler(this.PrevPage_Click);
this.DataList2.ItemCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.DataList2_ItemCommand);
this.DataList2.SelectedIndexChanged += new System.EventHandler(this.DataList2_SelectedIndexChanged);
this.NextPage.Click += new System.EventHandler(this.NextPage_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void DisplayResult(int record)
{

int pages;
int estResults,i;
int maxpages=10;
string keywords;

keywords= "site:"+Domain.Text+ " "+SearWords.Text ;
GoogleSearchResult r = GoogleSearch(keywords,record);
estResults = r.estimatedTotalResultsCount;
Results.Text = "Total Results Found:"+Convert.ToString(estResults);
DataTable Resdt = new DataTable();
DataRow Resdr;
Resdt.Columns.Add(new DataColumn("Order", typeof(string)));
Resdt.Columns.Add(new DataColumn("URL", typeof(string)));
Resdt.Columns.Add(new DataColumn("title", typeof(string)));
Resdt.Columns.Add(new DataColumn("snippet", typeof(string)));
Resdt.Columns.Add(new DataColumn("cachedSize", typeof(string)));
i=0;
foreach(ResultElement Res in r.resultElements)
{
i++;
Resdr = Resdt.NewRow();
if(Res.title.Trim()=="")
{
i--;
continue;
}
else
Resdr[0] = i.ToString();
Resdr[1]= Res.URL;
Resdr[2]= Res.title;
Resdr[3]= Res.snippet;
Resdr[4]= Res.cachedSize;
Resdt.Rows.Add(Resdr);
}

DataList1.DataSource =new DataView(Resdt);;
DataList1.DataBind();

pages=estResults/PageRows;
int currentpage = record/PageRows ;
int firstpage=0;
int lastpage=currentpage;

if(currentpage>pages)
SetCurrentRecord(pages*PageRows);
if(pages<=maxpages)
{
firstpage=0;
lastpage=pages+1;
}
else
{
if(currentpage+maxpages/2>pages)
{
lastpage=pages;
firstpage=pages-maxpages;
}
else
{
if((currentpage-maxpages/2)>0)
{
firstpage=currentpage-maxpages/2;

}
else
firstpage=0;

if((currentpage+maxpages/2)>pages)
lastpage=pages;
else
{
lastpage=currentpage+maxpages/2;
if((lastpage-firstpage)<maxpages)
lastpage=firstpage+maxpages;
}
}
}

DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("Key", typeof(Int32)));

for(i=firstpage+1;i<lastpage+1;i++)
{
if(pages==0)
break;

dr = dt.NewRow();
dr[0] = i;
dt.Rows.Add(dr);
}

this.SetFirstPage(firstpage);
this.SetCurrentRecord(record);
if(currentpage==0)
PrevPage.Visible=false;
else
PrevPage.Visible=true;
if((currentpage==lastpage-1)||(pages==0))
NextPage.Visible=false;
else
NextPage.Visible=true;

DataList2.DataSource=new DataView(dt);
DataList2.DataBind();

}

private void Search_Click(object sender, System.EventArgs e)
{
Results.Visible=true;
PrevPage.Visible=true;
NextPage.Visible=true;
DisplayResult(0);
}
private int GetViewState(string name)
{
return Int32.Parse(ViewState[name].ToString());
}

private void SetCurrentRecord(int record)
{
if (record < 0) record = 0;
ViewState["CurrentRecord"]= Convert.ToString(record);

}
private void SetFirstPage(int page)
{
if (page < 0) page = 0;
ViewState["FirstPage"]= Convert.ToString(page);

}


private void PrevPage_Click(object sender, System.EventArgs e)
{
int record = GetViewState("CurrentRecord") - PageRows;

if(record<0)
record=0;
this.DisplayResult(record);

}

private void NextPage_Click(object sender, System.EventArgs e)
{
int record = GetViewState("CurrentRecord") + PageRows;
this.DisplayResult(record);

}

private void DataList2_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{

int page = GetViewState("FirstPage") ;
this.DisplayResult((page+e.Item.ItemIndex)*PageRows);

}

private void DataList2_SelectedIndexChanged(object sender, System.EventArgs e)
{

}

private void SearWords_TextChanged(object sender, System.EventArgs e)
{
DisplayResult(0);
}
}
}

 

 

EaseSearch.aspx

<%@ Import Namespace="GoogleWebService" %>
<%@ Page language="c#" Codebehind="EaseSearch.aspx.cs" AutoEventWireup="false" Inherits="EaseSoft.Utitlity.EaseSearch" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Search YourDomain.com</title>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<meta content="Microsoft Visual Studio 7.0" name=GENERATOR>
<meta content=C# name=CODE_LANGUAGE>
<meta content=http://schemas.microsoft.com/intellisense/ie5 name=vs_targetSchema>
</HEAD>
<body bgColor=#ffffff leftMargin=0 topMargin=0><a name=top></a>
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
<tr vAlign=top>
<td vAlign=top>
<table cellSpacing=0 cellPadding=0 width="100%" border=0
>
<tr vAlign=top>
<td id=donotprint style="BORDER-RIGHT: #999999 1px solid" vAlign=top
width=178 bgColor=#f1f1f1></td>
<td width="100%">
<!-- contents start here -->
<!-- contents start here -->
<table cellSpacing=0 cellPadding=10 width="100%" border=0
>
<tr>
<td
style="PADDING-RIGHT: 20px; PADDING-LEFT: 20px; PADDING-BOTTOM: 20px; PADDING-TOP: 11px"
width="100%">
<table cellSpacing=0 cellPadding=0 width="100%" border=0
>
<tr>
<td>

<asp:label id=GoogleKey Visible="False" runat="server">License Key From Google</asp:label>

<asp:label id=Domain Visible="False" runat="server">www.yourdomain.com</asp:label>

</td></tr>
<tr>
<td></td></tr>
<tr>
<td>
<h1>Search
YourDomain.com</h1></td></tr></table>
<table cellSpacing=0 cellPadding=0 width="100%" border=0
>
<tr>
<td vAlign=top align=left>
<!-- -->
<form id=frmSiteSearch method=post
runat="server">
<P><FONT face=&#23435;&#20307;
></FONT>&nbsp;</P>
<P><asp:label id=Label2 runat="server" Font-Size="Medium" EnableViewState="False">Search for</asp:label><FONT
face=&#23435;&#20307;>&nbsp; </FONT><asp:textbox id=SearWords runat="server"></asp:textbox><asp:button id=Search runat="server" Text="Search"></asp:button>&nbsp;&nbsp;<asp:label id=Results runat="server">Label</asp:label><BR
><BR><asp:datalist id=DataList1 runat="server" BorderColor="Blue" Width="80%">
<SeparatorStyle ForeColor="Blue"></SeparatorStyle>
<ItemTemplate>
<table>
<tr>
<td valign="top" width="15" rowspan="4">
<%# DataBinder.Eval(Container.DataItem, "Order") %>
</td>
</tr>
<tr>
<td valign="top">
<A href='<%# DataBinder.Eval(Container.DataItem, "URL") %>'>
<%# DataBinder.Eval(Container.DataItem, "title") %>
</A>
</td>
</tr>
<tr>
<td>
<%# DataBinder.Eval(Container.DataItem, "snippet") %>
</td>
</tr>
<tr>
<td>
<A href='<%#DataBinder.Eval(Container.DataItem, "URL")%>'>
<%#DataBinder.Eval(Container.DataItem,"URL")%>
--<%# DataBinder.Eval(Container.DataItem,"cachedSize")%></A>
</td>
</tr>
<tr height="2">
<td>&nbsp;</td>
</tr>
</table>
</ItemTemplate>
</asp:datalist>
<p></p>
<P><FONT face=&#23435;&#20307;
>
<table>
<TR>
<td><asp:linkbutton id=PrevPage runat="server">Previous</asp:linkbutton></td>
<td><asp:datalist id=DataList2 runat="server" DESIGNTIMEDRAGDROP="689" RepeatColumns="10">
<ItemTemplate>
<asp:linkbutton Text='<%# DataBinder.Eval(Container.DataItem, "Key", "{0:d}") %>' CommandName="Select" style="color:darkred" runat="server" ID="Linkbutton1" NAME="Linkbutton1"/>
</ItemTemplate>
</asp:datalist></td>
<td><asp:linkbutton id=NextPage runat="server" DESIGNTIMEDRAGDROP="691">Next</asp:linkbutton></td></TR></table></FONT></form>
<p></p>
<!-- contents ends here--></td></tr></table></td></tr>
<tr vAlign=top
></tr></table></td></tr></table></td></tr></table>
</body>
</HTML>