asp.nettriggersupdatepanelpartial-page-refreshasynchronous-postback

Async trigger for an update panel refreshes entire page when triggering too much in too short of time


I have a search button tied to an update panel as a trigger like so:

<asp:Panel ID="CRM_Search" runat="server">
    <p>Search:&nbsp;<asp:TextBox ID="CRM_Search_Box" CssClass="CRM_Search_Box" runat="server"></asp:TextBox>
    <asp:Button ID="CRM_Search_Button" CssClass="CRM_Search_Button" runat="server" Text="Search" OnClick="SearchLeads" /></p>
</asp:Panel>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="CRM_Search_Button" />
    </Triggers>
    <ContentTemplate> 
     /* Content Here */
    </ContentTemplate>
</asp:UpdatePanel>

In my javascript I use jQuery to grab the search box and tie it's keyup to make the search button click:

    $($(".CRM_Search_Box")[0]).keyup(
        function () {
            $($(".CRM_Search_Button")[0]).click();
        }
    );

This works perfectly, except when I start typing too fast. As soon as I type too fast (my guess is if it's any faster than the data actually returns) the entire page refreshes (doing a postback?) instead of just the update panel.

I've also found that instead of typing, if I just click the button really fast it starts doing the same thing.

Is there any way to prevent it from doing this? Possibly prevent 2nd requests until the first has been completed? If I'm not on the right track then anyone have any other ideas?

Thanks,
Matt


Solution

  • Its is better to do something like that, and give him a breath :)

    I place a timer, so you wait for 250Ms before its send the click. I always do that, I never call the search right way because when some one type a word, actually there is no reason for the program to search all the time....

    Also this will avoid your problem that you have.

    var hTimeOut = null;
    $($(".CRM_Search_Box")[0]).keyup(
            function () {
                if(hTimeOut)
                    clearTimeout(hTimeOut);
                hTimeOut = setTimeout(function() { $($(".CRM_Search_Button")[0]).click(); }, 250);            
            }
    );