To display the search results in the ASP.NET GridView control instead of SearchResult, you can make use of the DataSet that is returned from the SearchAgent API.
In this example a simple GridView is used with paging enabled.
Eg. ASPX
<%@ Register TagPrefix="searchengine" Namespace="Keyoti.SearchEngine.Web" Assembly="Keyoti4.SearchEngine.Web" %> <%@ Page language="c#" Inherits="KeyotiSearchDemo_CSharp.Example1_Simple.GridView_Search" CodeFile="gridview-search.aspx.cs" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <html> <head> <title>GridView</title> </head> <body> <form id="Form1" method="post" runat="server"> <searchengine:SearchBox ID='SearchBox_NoQuery' runat='server' ResultPageURL='.'> </searchengine:SearchBox>
<asp:GridView runat="server" ID="gridView1" AllowPaging="True" onpageindexchanging="gridView1_PageIndexChanging"> </asp:GridView> </form> </body> </html>
CODEBEHIND
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace KeyotiSearchDemo_CSharp.Example1_Simple { /// <summary> /// Summary description for SearchResults. /// </summary> public partial class GridView_Search : System.Web.UI.Page { protected void Page_Load(object sender, System.EventArgs e) { if (Request.Params["QueryExpr"] != null && !IsPostBack) { BuildResultData(); } } private void BuildResultData() { //--Run the search //Please update this license key if it has expired, using //http://keyoti.com/products/evaluation-key-generator/default.aspx?SKU=SEWEBP.NET string license = "59546960665C68616A5B6C6B635C53253C3A403C3A40423D443F45444647444B494F4B2"; Keyoti.SearchEngine.Configuration configuration = new Keyoti.SearchEngine.Configuration(); configuration.IndexDirectory = MapPath("~/IndexDirectory"); Keyoti.SearchEngine.Search.SearchAgent searchAgent = new Keyoti.SearchEngine.Search.SearchAgent(license, configuration); //get results only for requested page Keyoti.SearchEngine.Search.SearchResult result = searchAgent.Search(Request.Params["QueryExpr"], 1 + (gridView1.PageIndex * gridView1.PageSize), ((gridView1.PageIndex + 1) * gridView1.PageSize)); //need to create a datatable to hold results DataTable resultTable = new DataTable(); //setup the same columns as came from search agent. for (int i = 0; i < result.DataSet.Tables[0].Columns.Count; i++) resultTable.Columns.Add(result.DataSet.Tables[0].Columns[i].ColumnName); int j = 0; for (int i = 0; i < result.NumberOfResults; i++) { if (i >= gridView1.PageIndex * gridView1.PageSize && i < ((gridView1.PageIndex + 1) * gridView1.PageSize)) { resultTable.Rows.Add(result.DataSet.Tables[0].Rows[j++].ItemArray); } else resultTable.Rows.Add(new object[result.DataSet.Tables[0].Columns.Count]);//dummy data to fill out results for pagination } //bind the results gridView1.DataSource = resultTable; gridView1.DataBind(); } protected void gridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { gridView1.PageIndex = e.NewPageIndex; BuildResultData(); }
...Designer code
}
}
The BuildResultData method runs the search using SearchAgent. The rest of the code is there to copy the data out the DataTable that came from ‘result’ and put it in a new DataTable. The trick to this part is that the GridView should be bound to a DataTable holding as many rows as there are total results – but for efficiency we use blank rows for data that isn’t visible. This saves the search engine generating results (and summaries) for rows that can’t be seen. |