Saturday, April 12, 2008

Using Yahoo Search in your Application

embeding Yahoo Search in our applications is very easy .

Yahoo.API.dll can be downloaded from below link

http://developer.yahoo.com/download/download.html

Add this as reference in your solution and use below code:


protected void Search()
{
// Search Code
string strSearch = "web services";

Label1.Text = "Search Result for " + strSearch + " ";

Yahoo.API.YahooSearchService yahoo = new Yahoo.API.YahooSearchService();
Yahoo.API.WebSearchResponse.ResultSet resultSet;
resultSet = yahoo.WebSearch("YahooExample", strSearch, "all", 10, 1, "any", true, true, "en");
StringWriter sw = new StringWriter();

foreach (Yahoo.API.WebSearchResponse.ResultType result in resultSet.Result)
{
sw.WriteLine("{0}
", result.Title, result.ClickUrl);
sw.WriteLine("{0}
", result.Summary);
sw.WriteLine("{0}
", result.Url);
sw.WriteLine("
");
}
txtContent.Text = sw.ToString();
}

Web Crawler

Using System.Net.WebClient class, we can easily send data to or recieve data from an URI.

Below is a simple example of a web crawler using System.Net.WebClient class.
This will check for anchor tags in specified URI and adds all links to a ListBox. After getting the link, we can use it for whatever purpose.


ASPX.CS
________
protected void btnSubmit_Click(object sender, EventArgs e)
{
string strURL = txtLink.Text;
System.Net.WebClient wc = new System.Net.WebClient();
byte[] data = wc.DownloadData(strURL);
mshtml.HTMLDocumentClass ms = new mshtml.HTMLDocumentClass();
string strHTML =System.Text.Encoding.ASCII.GetString(data);
mshtml.IHTMLDocument2 objMyDoc = (mshtml.IHTMLDocument2)ms;
objMyDoc.write(strHTML);

mshtml.IHTMLElementCollection ec = (mshtml.IHTMLElementCollection)objMyDoc.links;

for (int i = 0; i < ec.length; i++)
{
string strLink;
mshtml.HTMLAnchorElementClass objAnchor;
try
{
objAnchor = (mshtml.HTMLAnchorElementClass)ec.item(i, 0);
strLink = objAnchor.href;
lstLinks.Items.Add(strLink);
}
catch
{
continue;
}

}
}

where txtLink is textbox where we enter URI
ex: http://www.google.com // URI - starts from http://
lstLinks is ListBox and btnSubmit is Button

for getting System.Net.WebClient add reference to Microsoft HTML Object Library on your solution

Monday, April 7, 2008

Using ajax we can place any control inside AJAX Update panel and by raising events we can get the data from server without posting whole page. Suppose a textbox is placed in an AJAX Update panel with Autopost back property = true and having textchanged event. then when text is changed textChanged event is raised and if any logic is written that will be processed and only that control is rendered but not the whole page..

Only thing to be noticed is that in Triggers tag we cant give client side events like OnKeyPress.
But when some data to be fetched on key press we need to call RaiseCallBackEvent to call server side code having placed TextBox inside Update panel. this way server side code is executed which binds the grid. That grid and Textbox both should be inside panel.
From this post onwards i will keep on adding the problems i faced while developing applications, how i solved them, where did i search for solutions, was it helpful and lot of stuff....

First thing

The first thing i did after creating blog is copied the URL and pasted in my ToDo list text file.. :)

Narasimha