Custom Search Engine - Google
Default.aspx Page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="GoogleCustomSearchEngine_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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="pnlSeach" runat="server" DefaultButton="btnSearch">
<asp:TextBox ID="txtKey" runat="server" rel="Search.aspx?q={0}"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" />
</asp:Panel>
</div>
</form>
</body>
</html>
Default.aspx.cs Page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class GoogleCustomSearchEngine_Default : System.Web.UI.Page
{
protected void btnSearch_Click(object sender, EventArgs e)
{
string searchURL = String.Format(txtKey.Attributes["rel"], HttpUtility.UrlEncodeUnicode(txtKey.Text));
Response.Redirect(searchURL, true);
}
}
Show Result on this page
Search.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Search.aspx.cs" Inherits="GoogleCustomSearchEngine_Search" %>
<!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></title>
<link rel="stylesheet" href="http://www.google.com/cse/style/look/default.css" type="text/css" />
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
google.load('search', '1', { language: 'en' });
google.setOnLoadCallback(function () {
var customSearchControl = new google.search.CustomSearchControl('CUSTOM SEARCH ENGINE ID');
customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
customSearchControl.setLinkTarget(google.search.Search.LINK_TARGET_TOP); // target the parent iframe
customSearchControl.draw('cse');
customSearchControl.execute('<%= HttpUtility.HtmlEncode(Request.QueryString["q"]) %>');
}, true);
</script>
</head>
<body>
<form id="form2" runat="server">
<div id="cse" style="width: 100%;">
Loading</div>
</form>
</body>
</html>
Search.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class GoogleCustomSearchEngine_Search : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
}
Default.aspx Page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="GoogleCustomSearchEngine_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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="pnlSeach" runat="server" DefaultButton="btnSearch">
<asp:TextBox ID="txtKey" runat="server" rel="Search.aspx?q={0}"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" />
</asp:Panel>
</div>
</form>
</body>
</html>
Default.aspx.cs Page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class GoogleCustomSearchEngine_Default : System.Web.UI.Page
{
protected void btnSearch_Click(object sender, EventArgs e)
{
string searchURL = String.Format(txtKey.Attributes["rel"], HttpUtility.UrlEncodeUnicode(txtKey.Text));
Response.Redirect(searchURL, true);
}
}
Show Result on this page
Search.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Search.aspx.cs" Inherits="GoogleCustomSearchEngine_Search" %>
<!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></title>
<link rel="stylesheet" href="http://www.google.com/cse/style/look/default.css" type="text/css" />
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
google.load('search', '1', { language: 'en' });
google.setOnLoadCallback(function () {
var customSearchControl = new google.search.CustomSearchControl('CUSTOM SEARCH ENGINE ID');
customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
customSearchControl.setLinkTarget(google.search.Search.LINK_TARGET_TOP); // target the parent iframe
customSearchControl.draw('cse');
customSearchControl.execute('<%= HttpUtility.HtmlEncode(Request.QueryString["q"]) %>');
}, true);
</script>
</head>
<body>
<form id="form2" runat="server">
<div id="cse" style="width: 100%;">
Loading</div>
</form>
</body>
</html>
Search.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class GoogleCustomSearchEngine_Search : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
}
hai i am anwar tanha from swat
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteNice article, here I found how to configure Google custom search engine in ASP.Net MVC Implement custom search engine in MVC
ReplyDelete