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

依然范特西╮ 提交于 2019-12-02 08:16:48

问题


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


回答1:


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);            
        }
);


来源:https://stackoverflow.com/questions/2627339/async-trigger-for-an-update-panel-refreshes-entire-page-when-triggering-too-much

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!