Coding an AjaxPro.NET Based Search Engine for Your Website (Page 1 of 4 )
In yesterday's article, we began building a search engine with AJAX functionality to be embedded into a .NET-based web site. We set up the frameworks we planned to use and designed the database. In this part, the conclusion to the tutorial, we will get into the client- and server-side design, add a couple of optimizations to the code, and then test out our new search engine.
A
downloadable .rar file is available for this article.
The Server-side Design
Before putting the AjaxPro.NET framework into our application, we should get everything ready. Above all, we should add a reference to AjaxPro.2.dll to the project, which will result in generating a Bin folder. With this done, we will also have to modify the relevant part in the configure file-Web.config, as show in the following code snippet:
//......(omitted)
<system.web>
<httpHandlers>
<add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory,
AjaxPro" />
</httpHandlers>
......(omitted)
</system.web>;
Moreover, we should do something inside the "Search.aspx.cs" code file to register the current page class as the following:
[AjaxPro.AjaxNamespace("MySearchEngine")]
public partial class Search : System.Web.UI.Page{
private void Page_Load(object sender, System.EventArgs e){
Utility.RegisterTypeForAjax(typeof(Search));
}
Note the first line of code is quite different from that provided by early versions of the framework. Be careful!
With the above steps prepared, we can now write the Ajax methods. Let's focus on the first one, GetResultXml:
[AjaxPro.AjaxMethod]
public string GetResultXml(string strWord){
string strConnection = ConfigurationManager.ConnectionStrings
["MyConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(strConnection);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = string.Format(
"SELECT id, title, author FROM Article WHERE title LIKE '%{0}%'",strWord );
DataSet ds = new DataSet("ArticleDataSet");
SqlDataAdapter da = new SqlDataAdapter(cmd);
try{
da.Fill(ds);
}
catch (SqlException) {}
finally{
conn.Close();
}
return ds.GetXml();
}
First, according to the downloaded materials, we have to put the attribute [AjaxPro.AjaxMethod] before every method that will be called via the AjaxPro.NET framework from the client side. The several steps that follow are the typical ASP.NET 2.0 database operation. The first point to notice is that we name the DataSet object "ArticleDataSet," so that will be conveniently used later. In the last step, we convert the record data in table form into a XML string by calling the GetXml method of our created DataSet instance.
Now let's turn our attention to the next Ajax method, namely GetXsl:
[AjaxPro.AjaxMethod]
public string GetXsl(){
XmlDocument doc = new XmlDocument();
string strPath = Server.MapPath("../xsl/trans.xslt");
doc.Load(strPath);
return doc.InnerXml;
}
This method is relatively simple; its working relies on the XmlDocument class. We first get and load the required .xslt file, "trans.xslt," into the doc object. Then we directly return the XML-formatted string by returning the value of the InnerXml property of the doc object.
There is one final Ajax method: GetResultCount, which is responsible for returning the total number of the articles that satisfy the searching condition. Since it is also pretty simple, we will not dwell on it; please refer to the downloadable source code included at the beginning of this article.
Next: The Client-side Design >>
More .NET Articles
More By Xianzhong Zhu