How to Create Search Functionality in ASP.NET Website
As your site grows in pages, so does the time required for visitors to locate specific information on it. While it’s obvious to most the benefits of extending a site with search functionality, the ease in which this could be done may not be. Search box is an important thing for a website, for example is in an e-commerce site. An effective site search function on an e-commerce site has a number of potential benefits. Customers are accustomed to finding results quickly and (mainly) accurately from search engines, and will expect a similar experience on e-commerce sites. On e-commerce sites, up to 30% of visitors will use the site search box, and each of these users is showing a possible intent to purchase by entering product names or codes.
In this article we will be creating simple search functionality in ASP.NET. Once the user searches for information, we are going to query our DataBase using Entity Framework and display the results as links in the Client Template of RadSearchBox. The control will be populated with data via Web Service, where the returned data will be structured.
The DataBase
For the sake of the example, we are going to create a simple Table (ControlsTable) adding few columns: ControlID, ControlName, ControlContents and SectionName.
Querying the DataBase Using ASP.NET Entity Framework
ASP.NET Entity Framework is an Object/Relational Mapping (O/RM) framework, which provides an automated mechanism for accessing and storing the data in the database. It provides the ability for the developers to retrieve and manipulate data as strongly typed objects using C# or VB.Net.
1 2 3 4 5 6 | DataEntities db = new DataEntities(); var q = db.ControlsTables.Where(i => i.ControlName.Contains(context.Text.Trim()) || i.ControlContents.Contains(context.Text.Trim()) || i.SectionName.Contains(context.Text.Trim()) ).ToList(); |
Each time the user types a character in the input of the control a callback is triggered to the WebService and the underlying database is requested with the above query. The retrieved entries from the database would be the ones, which contain the value that was entered in the input.
Displaying the Results
In order to provide RadSearchBox with the needed, we have to manage the returned data source in the WebService in the following manner:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | [WebMethod] public SearchBoxData GetSearchResults(SearchBoxContext context) { List<SearchBoxItemData> resultSet = new List<SearchBoxItemData>(); DataEntities db = new DataEntities(); var q = db.RadControlsTables.Where(i => i.ControlName.Contains(context.Text.Trim()) || i.ControlContents.Contains(context.Text.Trim()) || i.SectionName.Contains(context.Text.Trim()) ).ToList(); foreach (var item in q) { string controlName = item.ControlName.ToString(); string sectionName = item.SectionName.ToString(); string url = string.Format("{0}/{1}/{2}/default.aspx", HttpContext.Current.Request.ApplicationPath, controlName, sectionName); SearchBoxItemData searchItemitem = new SearchBoxItemData(); searchItemitem.Text = controlName; searchItemitem.Value = url; searchItemitem.DataItem["Text"] = sectionName; resultSet.Add(searchItemitem); } SearchBoxData res = new SearchBoxData(); res.Items = resultSet.ToArray(); return res; } |
Then you could easily configure the ClientTemplate of ASP.NET SearchBox control, in order to evaluate the structured data:
1 2 3 4 5 6 | <ClientTemplate> <a href="#= Value #"> <span>#= DataItem.Text #</span> <span>#= Text #</span> </a> </ClientTemplate> |
User Experience
At the end, what we’ve achieved is quite flexible search functionality, using a WebService and ASP.NET Entity Framework, which serves out the result in a user-friendly manner.
Cheap ASP.NET 4.5 Hosting Recommendation
- Build Your ASP.NET Website
Use ASPHostPortal.com‘s website building tools to get that special, customized look for your website. A nifty wizard will walk you through the process. - All-inclusive Prices Unbeatable Value
Other companies promise cheap hosting, but then charge extra for setup fees, higher renewal rates, or promotional services. With ASPHostPortal.com, the listed price is the number you’ll pay, and you can expect a fully loaded, comprehensive suite of web services. - Fast and Secure Server
Their powerful servers are especially optimized and ensure the best ASP.NET 4.5 performance. They have best data centers on three continent and unique account isolation for security. - Easy to Use and Manage
ASPHostPortal.com webspace explorer lets you manage your website files with a browser. A control panel lets you set up and control your server functions with ease.