Displaying search results

Here is another example with paging and a content extract in the search result:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:TextBox runat="server" ID="txtSearch"></asp:TextBox>

        <asp:Button ID="btnSearch" OnClick="btnSearch_Click" runat="server" Text="Search" />

        <asp:Literal runat="server" ID="litResult" EnableViewState="false"></asp:Literal>

    </div>

    </form>

</body>

</html>


 

The output of this example is:

 

In the code behind file:

using System;

using System.Web;

using WAF.Presentation.Web;

using WAF.Engine;

using System.Text;

using WAF.Engine.Content;

 

public partial class _Default : System.Web.UI.Page {

    string _qSearch = "q";

    string _qPage = "page";

    int _pageSize = 10;

    protected void Page_Load(object sender, EventArgs e) {

        if (!IsPostBack) {

            string search = Request[_qSearch];

            if (search != null) {

                litResult.Text = getSearchHtml(search);

                txtSearch.Text = search;

            }

        }

    }

    protected void btnSearch_Click(object sender, EventArgs e) {

        txtSearch.Text = txtSearch.Text.Trim();

        if (txtSearch.Text.Length > 0) {

            var qs = _qSearch + "=" + HttpUtility.UrlEncode(txtSearch.Text);

            
Response.Redirect(WAFContext.GetUrl(WAFContext.Request.NodeId, qs));

        }

    }

    protected string getSearchHtml(string search) {

        StringBuilder html = new StringBuilder();

        if (search == null) return "";

        int pageIndex = 0;

        if (int.TryParse(Request["page"], out pageIndex)) pageIndex--;

        int pageCount;

        int totalCount;

        try {

            var query = new IndexQuery<ContentBase>();

            query.BodySearch = search;

            query.PageIndex = pageIndex;

            query.PageSize = _pageSize;

            var results = WAFContext.Session.Search(query, out pageCount, out totalCount);

            html.Append("<div style=\"margin-top:10px;\">Your search gave <strong>");

            html.Append(totalCount);

            html.Append("</strong> hit" + (totalCount == 1 ? "" : "s"));

            html.Append(": ");

            html.Append("</div>");

            if (pageIndex >= pageCount) pageIndex = pageCount - 1;

            string pagingHtml = getPageHtml(_pageSize, pageCount, pageIndex, search);

            if (pagingHtml != null) html.Append(pagingHtml);

            foreach (var result in results) {

                html.Append("<div style=\"margin-top:10px;\"><a href=\"");

                html.Append(HttpUtility.HtmlAttributeEncode(WAFContext.GetUrl(result.Content)));

                html.Append("\">");

                html.Append(result.FormatSample(HttpUtility.HtmlEncode(result.Name), 160,

                    "<span style=\"font-weight:bold; font-style:italic; font-size:16px;\">", "</span>"));

                html.Append("</a></div>");

                html.Append("<div style=\"font-size:12px;\">");

                html.Append(result.FormatSample(HttpUtility.HtmlEncode(result.Body), 160,

                    "<span style=\"font-weight:bold; font-style:italic; font-size:14px;\">", "</span>"));

                html.Append("</div>");

            }

            if (pagingHtml != null) html.Append(pagingHtml);

        } catch (Exception error) {

            html.Append("<div style=\"color:gray; margin-top:10px;\">INVALID SEARCH: " + error.Message + "</div>");

        }

        return html.ToString();

    }

    string getPageHtml(int pageSize, int pageCount, int pageIndex, string search) {

        if (pageCount < 2) return null;

        StringBuilder html = new StringBuilder();

        html.Append("<div style=\"margin-top:10px;\">");

        html.Append("PAGES: ");

        for (int i = 1; i <= pageCount; i++) {

            html.Append("<a style=\"padding: 3px;");

            if (i - 1 == pageIndex) html.Append("font-weight:bold;");

            html.Append("\" href=\"");

            var qs = _qSearch + "=" + HttpUtility.UrlEncode(search) + "&" + _qPage + "=" + i;

            html.Append(WAFContext.GetUrl(WAFContext.Request.NodeId, qs));

            html.Append("\">");

            html.Append("[");

            html.Append(i);

            html.Append("]");

            html.Append("</a>");

        }

        html.Append("</div>");

        return html.ToString();

    }

}