Sitefinity CMS

Setting Search Indexes at Runtime Send comments on this topic.
See Also
Developing with Sitefinity > Services > Search Service > Setting Search Indexes at Runtime

Glossary Item Box

This topic shows how to set search indexes at runtime depending on a selected value.

 

After creating several indexes for the search module, it is more convenient to set the index at runtime based on the value selected in a drop-down box. 

 

Following is a simple example how to achieve this. A user control wraps the SearchBox control that is shipped with Sitefinity:

.ascx Copy Code
<%@ Register Assembly="Telerik.Search" Namespace="Telerik.Search.WebControls" TagPrefix="sitefinity" %>

<div>
   
<asp:DropDownList ID="IndexCatalogue" runat="server" AutoPostBack="true" OnSelectedIndexChanged="SearchIndexes_SelectedIndexChanged"></asp:DropDownList>
</div>
<div>
   
<sitefinity:SearchBox ID="SearchBox1" runat="server" ResultUrl="~/ResultUrlPage.aspx"></sitefinity:SearchBox>
</div>  

 

.ascx.cs Copy Code
protected void Page_Load(object sender, EventArgs e)
{
 
if (!IsPostBack)
 {
   
this.BindIndexCatalogue();
 }
}

protected void SearchIndexes_SelectedIndexChanged(object sender, EventArgs e)
{
 
this.SearchBox1.IndexCatalogue = ((ListControl)sender).SelectedValue;
}

void BindIndexCatalogue()
{
 
this.IndexCatalogue.DataSource = IndexingManager.IndexingServices.Values;
 
this.IndexCatalogue.DataTextField = "Name";
 
this.IndexCatalogue.DataValueField = "Name";
 
this.IndexCatalogue.DataBind();

 
this.IndexCatalogue.Items.Insert(0, new ListItem("Please, select an index", string.Empty));
}  

 

In order to use it as a public control and be able to add it to a Sitefinity page, this user control should be uploaded in the toolbox.

 

See Also

Sitefinity Building Parts: Services
Search Overview
Search Public Controls