<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" Width="300px"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Google Search" OnClick="Button1_Click" /><br />
<asp:DataList ID="dlSearch" runat="server" Width="600px">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Text='<%#Eval("Title") %>'PostBackUrl='<%#Eval("Url") %>'></asp:LinkButton><br />
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Content") %>'></asp:Label><br />
<br />
</ItemTemplate>
<FooterTemplate>
<asp:Label Visible='<%#bool.Parse((dlSearch.Items.Count==0).ToString())%>' runat="server"
ID="lblNoRecord" Text="No Record Found!"></asp:Label>
</FooterTemplate>
</asp:DataList>
</div>
</form>
</body>
using System.Collections.Generic;
using Google.API.Search;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dlSearch.DataSource = null;
dlSearch.DataBind();
TextBox1.Text = "";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Title", typeof(string)));
dt.Columns.Add(new DataColumn("Content", typeof(string)));
dt.Columns.Add(new DataColumn("Url", typeof(string)));
GwebSearchClient client = new GwebSearchClient("www.c-sharpcorner.com");
IList<IWebResult> results = client.Search(TextBox1.Text, 32);
foreach (IWebResult result in results)
{
DataRow dr = dt.NewRow();
dr["Title"] = result.Title.ToString();
dr["Content"] = result.Content.ToString();
dr["Url"] = result.Url;
dt.Rows.Add(dr);
}
dlSearch.DataSource = dt;
dlSearch.DataBind();
}
}
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" Width="300px"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Google Search" OnClick="Button1_Click" /><br />
<asp:DataList ID="dlSearch" runat="server" Width="600px">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Text='<%#Eval("Title") %>'PostBackUrl='<%#Eval("Url") %>'></asp:LinkButton><br />
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Content") %>'></asp:Label><br />
<br />
</ItemTemplate>
<FooterTemplate>
<asp:Label Visible='<%#bool.Parse((dlSearch.Items.Count==0).ToString())%>' runat="server"
ID="lblNoRecord" Text="No Record Found!"></asp:Label>
</FooterTemplate>
</asp:DataList>
</div>
</form>
</body>
Now go to the code view.
Next add a reference of the following Google Search API DLL to your website:
- GoogleSearchAPI.dll
using System.Collections.Generic;
using Google.API.Search;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dlSearch.DataSource = null;
dlSearch.DataBind();
TextBox1.Text = "";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Title", typeof(string)));
dt.Columns.Add(new DataColumn("Content", typeof(string)));
dt.Columns.Add(new DataColumn("Url", typeof(string)));
GwebSearchClient client = new GwebSearchClient("www.c-sharpcorner.com");
IList<IWebResult> results = client.Search(TextBox1.Text, 32);
foreach (IWebResult result in results)
{
DataRow dr = dt.NewRow();
dr["Title"] = result.Title.ToString();
dr["Content"] = result.Content.ToString();
dr["Url"] = result.Url;
dt.Rows.Add(dr);
}
dlSearch.DataSource = dt;
dlSearch.DataBind();
}
}
In the code above i passed the textbox value in button click to google server.
After getting the result i binded it to datatable then to datalist control.
just check these two lines:
GwebSearchClient client = new GwebSearchClient("www.asp.net");
IList<IWebResult> results = client.Search(TextBox1.Text, 30);
IList<IWebResult> results = client.Search(TextBox1.Text, 30);
In the 1st line i am passing "www.asp.net" as Client because it required a hosted site for Security purposed.
If you didn't pass then it will show exception.
In the 2nd line i am passing 30 and textbox value. Here 30 means i am getting 30 search results.
So You Can increase or decrease this value according to your requirement.
Now build your application. Enter Search query press the button.
Comments
Post a Comment