Clear Smart Search Filters Button

Tracey Penberthy asked on June 14, 2017 09:59


I have a search page containing a smart search dialog, smart search results and multiple smart search filters I am trying to create a clear smart search filters button to reset the the filters.

I thought I could look for all the smart search filters on a page and reset them but I am struggling to do this as I cannot find a way to access the smart search filter in code behind, I get this far and am stuck

private void BtnClearFilters_Click(object sender, EventArgs e)
    var parentControl = this.Parent;
        if(null != parentControl)
            foreach(var ctl in parentControl.Controls)
                if(ctl.GetType().ToString() == "ASP.cmswebparts_smartsearch_searchfilter_ascx")
                    var f = ctl as CMSAbstractWebPart;
                    // how do I cast as a smart search filter?

Is this the way to go about it? If so can anyone give me pointers.

Or should I look at doing this with javascript?

Many Thanks

Correct Answer

Trevor Fayas answered on June 14, 2017 16:02

I would just use javascript to clear filters, going code behind is a bit overkill.

You can sometimes cast a control, but if it's not a compiled level it can be very difficult to do. You have the right idea to cast as a CMSAbstractWebPart, that should give you access to the SetValue and GetValue, if these are tied in any way to the textbox (if you can set the value to nothing) that may work.

f.SetValue("Value", "");

You'll have to see in the control what value is tied to the filter.

Note you'll also, if you want to continue this path, do a recursive search of controls, as the .Controls only list the child controls, not grandchildren. There is a way i believe to get a webpart by it's WebpartID, i believe it's in the CMS.PortalEngine.PageTemplateInstance.GetWebPart(string webpartID), or near it, and you may need to first get the current PageTemplateInstance, and i'm not sure if it returns the current control or just the info of it.

0 votesVote for this answer Unmark Correct answer

Recent Answers

Tracey Penberthy answered on June 14, 2017 16:13

Thanks Trevor

I think you are right I am overcomplicating things.

I managed to use javascript to clear them by clearing the querystrings from the url and reloading the page.

$("#clearFilterButton").on("click", function () {
        var url = window.location.href;
        var qsInd = url.indexOf("?");
        if (qsInd > 0) {
            // remove querystring
            var newurl = url.substring(0, qsInd);
            window.location.href = newurl;
0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on June 14, 2017 16:14

Awesome, make sure if the question is answered, to set it as such so others looking to answer questions don't go through this!

0 votesVote for this answer Mark as a Correct answer

Ilesh Mistry answered on June 15, 2017 09:35 (last edited on June 15, 2017 09:35)

Just an alternative to the JS solution, if you know the URL of the page, then you can avoid JS altogether and go for the

<a href='/location of page without search filter parameters'>Clear search filters</a>
<button href='/location of page without search filter parameters'>Clear search filters</button>
2 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.