Search content within the blog

Tuesday, March 31, 2009

Searching contents in our own website using google API

Initially create a custom search engine by going to the following link
Click here . Once you get your custom id of the search then add a tectbox and button in the form in which u want to perform search as follows

ASPX Code
<asp:TextBox runat="server" ID="txtSrch" Width="155px" />

<asp:Button runat="server" CausesValidation="true" ID="btnSubmit" Text="Go" OnClick="btnSubmit_Click" />

Then add the following in button click event code
ASPX.CS

protected void btnSubmit_Click(object Sender, EventArgs e)
{
Response.Redirect("SearchSelfhelp.aspx?cx=016016947991373917210:1lu0mykq3dw&cof=FORID:10&ie=UTF-8&q=" + Server.UrlEncode(txtSrch.Text.Trim()) + "&sa=Search");
}

here SearchSelfhelp.aspx is the place ur result will be shown and cx=XXX where XXX is your unique search number given when you register ur search and similarly we also obtain FORID.

just make sure you encode the contents of the search textbox.

And in the page where results will be displayed that is SearchSelfhelp.aspx
insert the following code

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

No comments:

Post a Comment