Gravatar for achmedrauff.abdulrahim@3ds.com

Question by Achmed Rauff, Apr 21, 2015 1:01 AM

Not to display results when search box is empty.

I have an interface that is required to NOT display results when the search box is empty. Is there away I can do this via the .Net UI?

I have already changed the "Always execute query" but all results still displays by default.

Any ideas?

3 Replies
Gravatar for rquirion@coveo.com

Answer by ronald, Apr 21, 2015 11:57 AM

I don't know very much about your setup. For example, are you looking for a solution only for the initial display of the search page? Has your skin been customized? Etc. Your need could probably be addressed in a few ways.

Here is a technique that should work. Open the ResultsPanel.ascx file of your skin in a text editor. Add the ID and runat attributes to the main control. For example:

 <table . . . id="pnl" runat="server">
</table>

And add the following piece of code at the end of the file:

<script runat="server">
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        if (SearchObject.State.Staging[Coveo.CES.Web.Search.Settings.QuerySettings.NAME] == null &&
                SearchObject.State.Mode == ModeEnum.Normal) {
            if (pnl.Style["visibility"] == null) {
                pnl.Style.Add("visibility", "hidden");
                Coveo.CNL.Web.Ajax.AjaxManager.Current.RegisterControlToUpdate(pnl, Coveo.CNL.Web.Ajax.EffectNone, null);
            }
        } else {
            if (pnl.Style["visibility"] != null) {
                pnl.Style.Remove("visibility");
                Coveo.CNL.Web.Ajax.AjaxManager.Current.RegisterControlToUpdate(pnl, Coveo.CNL.Web.Ajax.EffectNone, null);
            }
        }
    }
</script>
Gravatar for achmedrauff.abdulrahim@3ds.com

Comment by Achmed Rauff, Apr 29, 2015 4:18 AM

I have tried but there seems to be an error in this line below;

pnl.Style.Remove('visibility");

changing it to

pnl.Style.Remove("visibility");

states an error in the following line;

if (SearchObject.State.Staging[Coveo.CES.Web.Search.Settings.QuerySettings.NAME] == null &&
Gravatar for rquirion@coveo.com

Comment by ronald, Apr 29, 2015 3:13 PM

The single-quote had to be a double-quote. It's correct to have changed it. (I corrected it in my answer.) Regarding the remaining point, what is the error exactly? Could it be a copy+paste issue because of the line break?

Gravatar for achmedrauff.abdulrahim@3ds.com

Comment by Achmed Rauff, May 11, 2015 2:32 AM

I am getting the following error.

CS0234: The type or namespace name 'QuerySettings' does not exist in the namespace 'Coveo.CES.Web.Search.Settings' (are you missing an assembly reference?)

Gravatar for achmedrauff.abdulrahim@3ds.com

Answer by Achmed Rauff, May 11, 2015 3:33 AM

I have updated your code as there were errors in it:

<script runat="server">
protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    if (SearchObject.State.Staging[Coveo.CES.Web.Search.Settings.QuerySetting.NAME] == null &&
            SearchObject.State.Mode == ModeEnum.Normal) {
        if (pnl.Style["visibility"] == null) {
            pnl.Style.Add("visibility", "hidden");
            Coveo.CNL.Web.Ajax.AjaxManager.Current.RegisterControlToUpdate(pnl, Coveo.CNL.Web.Ajax.EffectEnum.None);
        }
    } else {
        if (pnl.Style["visibility"] != null) {
            pnl.Style.Remove("visibility");
            Coveo.CNL.Web.Ajax.AjaxManager.Current.RegisterControlToUpdate(pnl, Coveo.CNL.Web.Ajax.EffectEnum.None);
        }
    }
}
</script>

But now I have come to another error CS1501: No overload for method 'RegisterControlToUpdate' takes '2' arguments

Coming from this line: Coveo.CNL.Web.Ajax.AjaxManager.Current.RegisterControlToUpdate(pnl, Coveo.CNL.Web.Ajax.EffectEnum.None);

Gravatar for jflheureux@coveo.com

Comment by Jean-François L'Heureux, May 12, 2015 11:34 AM

In Ronald's example, he passes a third argument to the RegisterControlToUpdate method which is null. Can you try that?

Gravatar for achmedrauff.abdulrahim@3ds.com

Comment by Achmed Rauff, May 13, 2015 5:17 AM

Awesome!! It works! Thank you. Below is the corrected code for reference.

Gravatar for achmedrauff.abdulrahim@3ds.com

Answer by Achmed Rauff, May 13, 2015 5:17 AM

<script runat="server">
protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    if (SearchObject.State.Staging[Coveo.CES.Web.Search.Settings.QuerySetting.NAME] == null &&
            SearchObject.State.Mode == ModeEnum.Normal) {
        if (pnl.Style["visibility"] == null) {
            pnl.Style.Add("visibility", "hidden");
            Coveo.CNL.Web.Ajax.AjaxManager.Current.RegisterControlToUpdate(pnl, Coveo.CNL.Web.Ajax.EffectEnum.None, null);
        }
    } else {
        if (pnl.Style["visibility"] != null) {
            pnl.Style.Remove("visibility");
            Coveo.CNL.Web.Ajax.AjaxManager.Current.RegisterControlToUpdate(pnl, Coveo.CNL.Web.Ajax.EffectEnum.None, null);
        }
    }
}
</script>
Ask a question