Cancel Postback for Search

7 posts, 1 answers
  1. Kyle Smith
    Kyle Smith avatar
    42 posts
    Member since:
    Feb 2012

    Posted 11 Mar 2013 Link to this post

    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?
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 13 Mar 2013 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Kyle Smith
    Kyle Smith avatar
    42 posts
    Member since:
    Feb 2012

    Posted 13 Mar 2013 Link to this post

    Great. Thank you.
  5. Chris Salas
    Chris Salas avatar
    46 posts
    Member since:
    Oct 2009

    Posted 16 Aug 2013 Link to this post

    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.
  6. Chris Salas
    Chris Salas avatar
    46 posts
    Member since:
    Oct 2009

    Posted 19 Aug 2013 Link to this post

    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>

  7. Albert Shenker
    Albert Shenker avatar
    550 posts
    Member since:
    Mar 2004

    Posted 27 Dec 2013 Link to this post

    Really? A whole bunch of javascript instead of an AutoPostback property like is available on every other Asp.Net server event control?
  8. Plamen
    Admin
    Plamen avatar
    2731 posts

    Posted 01 Jan 2014 Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017