library

  for webmasters
  api
  security
  release notes

newsletter
Subscribe via RSS or via email
            

How to add a search form to your site

A simple technique for adding a search function to your site with QuickelSoft CMS.

Adding the search text box in your page

1. Do not forget to use the “QuickelSoftCMS:Form” if the search form is included in a page managed by QuickelSoft CMS. See the paragraph “Web Form and the HTTP module” in the Developper Guide.

2. Add a text box called TextBoxSearch.

3. Add a button called ButtonSearch.

<asp:TextBox ID="TextBoxSearch" Runat=server />
<asp:Button Text="Go" ID="ButtonSearch" OnClick="ButtonSearch_OnClick" Runat=server />   

4. In the ButtonSearch_OnClick method, add the following code. It will redirect the user to the search result page if the search query is not empty.

private void ButtonSearch_Click(object sender, EventArgs e)
{
 if (TextBoxSearch.Text != "") Response.Redirect(Root + "/search.aspx?Query=" +
Server.UrlEncode(TextBoxSearch.Text));
}

Display the search result

1. Create a search.aspx web form. This page will display in a repeater the list of documents matching the query.

2. Add a repeater called “RepeaterSearchResult”

<asp:Repeater ID="RepeaterSearchResult" Runat=server>
 <ItemTemplate>
 <a href="<%# DataBinder.Eval(Container.DataItem, "CurrentLanguageURL") %>">
 <%# DataBinder.Eval(Container.DataItem, "Title") %>
 </a>
 <br>
 </ItemTemplate>
</asp:Repeater>

3. In the code behind, inherit the page from the class QuickelSoft.CMS.API.Page

public class Search : QuickelSoft.CMS.API.Page

4. Bind the repeater data source to the search result.
The code uses the root folder and all its subfolders (IncludeSubFolders = true) to search for published content item matching the query.

private void Page_Load(object sender, System.EventArgs e)
{
 String query = Request["Query"];
 ContentItemCollection contentItems = CurrentSite.RootFolder.GetContentItems();
 contentItems.IncludeSubFolders = true;
 contentItems.Search(query);
 RepeaterSearchResult.DataSource = contentItems;
 RepeaterSearchResult.DataBind();
}
Note: The search results displays the documents matching the search query in the content items of the folder and the content items of the subfolders.
Related articles

Why does QuickelSoft CMS use a prefix with ASPX in each URL?
How to install automatically QuickelSoft CMS
How to use the QuickelSoft CMS API from a .Net application
API Error Codes
How to generate a sitemap for search engines
How to add a RSS feed to your site
QuickelSoft CMS 2.0.5 Documentation