This is a migrated thread and some comments may be shown as answers.

Cancel Postback for Search

6 Answers 199 Views
SearchBox
This is a migrated thread and some comments may be shown as answers.
Kyle Smith
Top achievements
Rank 1
Kyle Smith asked on 11 Mar 2013, 04:16 PM
I have a few cases where I want to cancel the postback of the Search Box. I am trying to use the OnClientSearch, but this doesn't appear to be possible. Am I overlooking something?

6 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 13 Mar 2013, 05:48 AM
Hello Kyle,

You can cancel the postback of the RadSearchBox in the OnClientSearch event. Please have a look into the following code which works fine at my end.

ASPX:
<telerik:RadSearchBox ID="RadSearchBox1" runat="server" OnClientSearch="OnClientSearch"
    EmptyMessage="Please type search key" onsearch="RadSearchBox1_Search">
</telerik:RadSearchBox>

JavaScript:
<script type="text/javascript">
    function OnClientSearch(sender, args) {
        if (sender.get_text().length < 1) {
            sender._element.control._postBackOnSearch = false;
        }
    }
</script>

Thanks,
Princy.
0
Kyle Smith
Top achievements
Rank 1
answered on 13 Mar 2013, 01:13 PM
Great. Thank you.
0
Chris Salas
Top achievements
Rank 1
answered on 16 Aug 2013, 08:35 PM
Is there a way to do this without the Sender object?  I am trying to catch the Enter event and redirect based on the highlighted listitem in the drowdown.
0
Chris Salas
Top achievements
Rank 1
answered on 19 Aug 2013, 02:33 PM
I have figured my issues and thought i would share.  I ended up checking if an item was highlighted and pulling an HREF attribute from the template and if no sender then redirecting to a search page and pulling the text from the input.
function Search_ClientSearch(sender, eventArgs) {
    if (sender) {
        if ($('li.rsbListItem.rsbListItemTemplate.rsbListItemHovered').length > 0) {
            sender._element.control._postBackOnSearch = false;
            $('li.rsbListItem.rsbListItemTemplate.rsbListItemHovered span.searchText').children().each(function (i) {
                $telerik.$('#radSBKeywords_Input')[0].value = $(this).text();
                window.location = $(this).attr('href');
            });
        }
    }
    else {
        window.location = "/search/results_mss.aspx?q=" + $telerik.$('#radSBKeywords_Input')[0].value;
    }
}

Here is the markup:
<telerik:RadSearchBox ID="radSBKeywords" runat="server" Skin="MetroTouch" OnClientSearch="Search_ClientSearch" OnSearch="btnSearch_Click" ClientIDMode="Static">
    <WebServiceSettings Path="../../WebServices/Telerik_WS.asmx" Method="GetSearchItems" />
    <DropDownSettings Height="300">
        <HeaderTemplate>
            <b><a href="javascript:ClickSearchButton();" >See All Results or Choose Below</a></b>
        </HeaderTemplate>
        <ClientTemplate>
            <span class="searchText">
                <a href="#= HRef #">#= Type # - #= Name #</a>
            </span>
        </ClientTemplate>
    </DropDownSettings>
</telerik:RadSearchBox>

0
Albert Shenker
Top achievements
Rank 1
Veteran
Iron
answered on 27 Dec 2013, 02:19 PM
Really? A whole bunch of javascript instead of an AutoPostback property like is available on every other Asp.Net server event control?
0
Plamen
Telerik team
answered on 01 Jan 2014, 01:33 PM
Hi Albert,

Yes indeed such  property would be a good improvement of the control. I have created added a new  feature request for its implementation and you can follow it and vote for it here

Meanwhile the code provided by Princy should work correctly too:
function OnClientSearch(sender, args) {
                    if (sender.get_text().length < 1) {
                        sender._element.control._postBackOnSearch = false;
                    }
                    window.location.href = "http://www.telerik.com";
                }

Regards,
Plamen
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
SearchBox
Asked by
Kyle Smith
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Kyle Smith
Top achievements
Rank 1
Chris Salas
Top achievements
Rank 1
Albert Shenker
Top achievements
Rank 1
Veteran
Iron
Plamen
Telerik team
Share this question
or