Knowledgebase Home Page  >  SearchUnit
Search the Knowledge Base
How can I set the templates programmatically? (C#)
https://keyoti.com/kb/Default.aspx?ToDo=view&questId=189&catId=54

Options

Print this page
Email this to a friend
Usually the SearchResult Control's templates are modified inside the ASPX, however if you are adding SearchResult programmatically, then you must set the templates differently (or use our default templates).
 
This is done using a CompiledTemplateBuilder, which uses your own delegate to create the template at runtime.  The delegate is called with the 'container' control (ResultItem, Header, Footer) which contains the data for display.
 
 
Eg.
 
 
//CODE TO ADD CONTROL TO PAGE
Keyoti.SearchEngine.Web.SearchResult sr = new SearchResult();
sr.IndexDirectory = m_IndexDirectory;
sr.LicenseKey = m_LicenseKey;
sr.NumberOfResultsPerPage = 10;

sr.HeaderTemplate = new CompiledTemplateBuilder(new BuildTemplateMethod(BuildSearchResultHeaderTemplate));
sr.ResultItemTemplate = new CompiledTemplateBuilder(new BuildTemplateMethod(BuildSearchResultItemTemplate));
sr.FooterTemplate = new CompiledTemplateBuilder(new BuildTemplateMethod(BuildSearchResultFooterTemplate));


//CODE TO MODIFY TEMPLATES
private void BuildSearchResultHeaderTemplate(Control container)
{
    Keyoti.SearchEngine.Web.Header head = container as Keyoti.SearchEngine.Web.Header;
    Literal litHeader = new Literal();
    litHeader.Text = @"<div class=""SearchResultHeader"">Showing result page " + head.ResultsPage + ". There are " + head.NumberOfResults + @" search results for """ + head.QueryExpression + @"""</div>";
    litHeader.Text += @"<table class=""SearchResults"">";
   
    container.Controls.Add(litHeader);
}

private void BuildSearchResultItemTemplate(Control container)
{
    Literal litResultItem = new Literal();
    litResultItem.DataBinding += new EventHandler(
        delegate
        {
            Keyoti.SearchEngine.Web.ResultItem item = container as Keyoti.SearchEngine.Web.ResultItem;
            litResultItem.Text = @"<tr><td class=""SearchResultItem"">";
            litResultItem.Text += @"<p><span class=""SearchResultTitle"">><a href=""" + item.Uri + @""">" + item.Title + "</a></span>";
            litResultItem.Text += @"<br />" + item.Summary;
            litResultItem.Text += @"<br /><span class=""SearchResultUrl"">" + item.Uri + "</span></p>";
            litResultItem.Text += @"<td></tr>" + Environment.NewLine;
        });

    container.Controls.Add(litResultItem);
}

private void BuildSearchResultFooterTemplate(Control container)
{
    Keyoti.SearchEngine.Web.Footer foot = container as Keyoti.SearchEngine.Web.Footer;
    Literal litFooter = new Literal();
    litFooter.Text = @"<tr><td class=""SearchResultFooter"">Result Page: ";
    litFooter.Text += foot.PageLinksBlock;
    litFooter.Text += @"</td></tr></table>";

    container.Controls.Add(litFooter);
}


Related Questions:

Attachments:

No attachments were found.