Google CSE Search Box

How to insert Google CSE Search Box in my site using an aspx file

Page 1 of 1

1 Replies - 10850 Views - Last Post: 17 March 2009 - 06:44 PM Rate Topic: -----

#1 Rinkey  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 24-February 09

Google CSE Search Box

Post icon  Posted 09 March 2009 - 12:03 AM

Please help, my code is as follow!

Default Page

<%@ Page Language="C#" EnableEventValidation="true" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
<!-- SAMPLE CODE ONLY - REPLACE WITH YOUR OWN -->
<!-- Google CSE Search Box Begins -->
<form id="searchbox_014373149466545347614:dygl7dqicp4" action="Default.aspx">
  <input type="hidden" name="cx" value="014373149466545347614:dygl7dqicp4" />
  <input name="q" type="text" size="40" />
  <input type="submit" name="sa" value="Search" />
  <input type="hidden" name="cof" value="FORID:9" />
</form>
<script type="text/javascript" 
  src="http://www.google.com/coop/cse/brand?form=searchbox_014373149466545347614%3Adygl7dqicp4"></script>
<!-- Google CSE Search Box Ends -->
 
 
 
<form action="http://www.freetrans.gov.za/SearchResults.aspx" id="cse-search-box">
  <div>
	<input type="hidden" name="cx" value="015382201262677853382:-ehqw50cmpu" />
	<input type="hidden" name="cof" value="FORID:10" />
	<input type="hidden" name="ie" value="UTF-8" />
	<input type="text" name="q" size="31" />
	<input type="submit" name="sa" value="Search" />
  </div>
</form>
 
<script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&lang=en"></script>
<asp:TextBox ID="q" MaxLength="512" Width="275px" AutoPostBack="false" runat="server" />
<asp:Button ID="_btnSearch" Text="Google Search" onclick="_btnSearch_Click" runat="server" />
<asp:RequiredFieldValidator ID="_rfvQ" ControlToValidate="q" runat="server" />
 
<asp:HiddenField ID="cx" Value="014373149466545347614:dygl7dqicp4" runat="server" />
<asp:HiddenField ID="cof" Value="FORID:9" runat="server" />
 

</asp:Content>
 
SearchResults
 
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="SearchResults.aspx.cs" Inherits="SearchResults" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
   <!-- SAMPLE CODE ONLY - REPLACE WITH YOUR OWN -->
<!-- Google Search Result Snippet Begins -->
<div id="results_014373149466545347614:dygl7dqicp4"></div>
<script type="text/javascript">
  var googleSearchIframeName = "results_014373149466545347614:dygl7dqicp4";
  var googleSearchFormName = "searchbox_014373149466545347614:dygl7dqicp4";
  var googleSearchFrameWidth = 600;
  var googleSearchFrameborder = 0;
  var googleSearchDomain = "www.google.com";
  var googleSearchPath = "/cse";
</script>
<script type="text/javascript" src="http://www.google.com/afsonline/show_afs_search.js"></script>
<!-- Google Search Result Snippet Ends -->
 
<div id="cse-search-results"></div>
<script type="text/javascript">
  var googleSearchIframeName = "cse-search-results";
  var googleSearchFormName = "cse-search-box";
  var googleSearchFrameWidth = 600;
  var googleSearchDomain = "www.google.com";
  var googleSearchPath = "/cse";
</script>
<script type="text/javascript" src="http://www.google.com/afsonline/show_afs_search.js"></script>
 
 
 
</asp:Content>
 
Code Behind
 
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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)
	{
		if (!IsPostBack)
		{
			String query = Request.QueryString["q"];
			if (!String.IsNullOrEmpty(query))
			{
				   // q.Text = SanitizeUserInput (HttpUtility.UrlDecode (query.Trim ()));
			}
		}
	}
 
	protected void _btnSearch_Click(Object sender, EventArgs e)
	{
		if (!IsValid)
			return;
 
		Response.Redirect(
			String.Format(
				"Default.aspx?q={0}&cx={1}&cof={2}",
		   //  HttpUtility.UrlEncode(SanitizeUserInput(q.Text.Trim())),
				HttpUtility.UrlEncode(cx.Value),
				HttpUtility.UrlEncode(cof.Value)
				),
			false
			);
 
		Context.ApplicationInstance.CompleteRequest();
	}
 
}


** Edit ** :code:

Is This A Good Question/Topic? 1

Replies To: Google CSE Search Box

#2 NARKOZ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-September 08

Re: Google CSE Search Box

Posted 17 March 2009 - 06:44 PM

Check this:

<%@ Page Language="C#" EnableEventValidation="true" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
<!-- SAMPLE CODE ONLY - REPLACE WITH YOUR OWN -->
<!-- Google CSE Search Box Begins -->
<form id=""searchbox_014373149466545347614:dygl7dqicp4"" action=""Default.aspx"">
  <input type=""hidden"" name=""cx"" value=""014373149466545347614:dygl7dqicp4"" />
  <input name=""q"" type=""text"" size=""40"" />
  <input type=""submit"" name=""sa"" value=""Search"" />
  <input type=""hidden"" name=""cof"" value=""FORID:9"" />
</form>
<script type=""text/javascript""
  src=""http://www.google.com/coop/cse/brand?form=searchbox_014373149466545347614%3Adygl7dqicp4""></script>
<!-- Google CSE Search Box Ends -->



<form action=""http://www.freetrans.gov.za/SearchResults.aspx"" id=""cse-search-box"">
  <div>
	<input type=""hidden"" name=""cx"" value=""015382201262677853382:-ehqw50cmpu"" />
	<input type=""hidden"" name=""cof"" value=""FORID:10"" />
	<input type=""hidden"" name=""ie"" value=""UTF-8"" />
	<input type=""text"" name=""q"" size=""31"" />
	<input type=""submit"" name=""sa"" value=""Search"" />
  </div>
</form>

<script type=""text/javascript"" src=""http://www.google.com/coop/cse/brand?form=cse-search-box&lang=en""></script>
<asp:TextBox ID="q" MaxLength="512" Width="275px" AutoPostBack="false" runat="server" />
<asp:Button ID="_btnSearch" Text="Google Search" onclick="_btnSearch_Click" runat="server" />
<asp:RequiredFieldValidator ID="_rfvQ" ControlToValidate="q" runat="server" />

<asp:HiddenField ID="cx" Value="014373149466545347614:dygl7dqicp4" runat="server" />
<asp:HiddenField ID="cof" Value="FORID:9" runat="server" />


</asp:Content>

SearchResults

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="SearchResults.aspx.cs" Inherits="SearchResults" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
   <!-- SAMPLE CODE ONLY - REPLACE WITH YOUR OWN -->
<!-- Google Search Result Snippet Begins -->
<div id=""results_014373149466545347614:dygl7dqicp4""></div>
<script type=""text/javascript"">
  var googleSearchIframeName = ""results_014373149466545347614:dygl7dqicp4"";
  var googleSearchFormName = ""searchbox_014373149466545347614:dygl7dqicp4"";
  var googleSearchFrameWidth = 600;
  var googleSearchFrameborder = 0;
  var googleSearchDomain = ""www.google.com"";
  var googleSearchPath = ""/cse"";
</script>
<script type=""text/javascript"" src=""http://www.google.com/afsonline/show_afs_search.js""></script>
<!-- Google Search Result Snippet Ends -->

<div id=""cse-search-results""></div>
<script type=""text/javascript"">
  var googleSearchIframeName = ""cse-search-results"";
  var googleSearchFormName = ""cse-search-box"";
  var googleSearchFrameWidth = 600;
  var googleSearchDomain = ""www.google.com"";
  var googleSearchPath = ""/cse"";
</script>
<script type=""text/javascript"" src=""http://www.google.com/afsonline/show_afs_search.js""></script>



</asp:Content>

Code Behind

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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)
	{
		if (!IsPostBack)
		{
			String query = Request.QueryString["q"];
			if (!String.IsNullOrEmpty(query))
			{
				   // q.Text = SanitizeUserInput (HttpUtility.UrlDecode (query.Trim ()));
			}
		}
	}

	protected void _btnSearch_Click(Object sender, EventArgs e)
	{
		if (!IsValid)
			return;

		Response.Redirect(
			String.Format(
				"Default.aspx?q={0}&cx={1}&cof={2}",
		   //  HttpUtility.UrlEncode(SanitizeUserInput(q.Text.Trim())),
				HttpUtility.UrlEncode(cx.Value),
				HttpUtility.UrlEncode(cof.Value)
				),
			false
			);

		Context.ApplicationInstance.CompleteRequest();
	}

}


;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1