Calling Web service twice or more when enter keyword button twice or more http://forums.asp.net/t/1788434.aspx/1?Calling+Web+service+twice+or+more+when+enter+keyword+button+twice+or+more+Wed, 04 Apr 2012 03:19:58 -040017884344913087http://forums.asp.net/p/1788434/4913087.aspx/1?Calling+Web+service+twice+or+more+when+enter+keyword+button+twice+or+more+Calling Web service twice or more when enter keyword button twice or more <p>Hello&nbsp;</p> <p>I want to prevent calling web service twice or more&nbsp;when enter keyword button twice or more&nbsp;</p> <p>So how to prevent it.</p> <p>My code like this</p> <p>function jWDGetTranslation() {</p> <p>&#36;(&quot;#div_ShowMsg&quot;).hide();</p> <p>&#36;(&quot;#showMore&quot;).hide();</p> <p>&#36;('#list').empty();<br> &nbsp; var txtSearchText = &#36;(&quot;#&lt;%= txtSearchText.ClientID %&gt;&quot;)<br> var activeDictionaryType = '&lt;%=ActiveDictionaryType %&gt;';<br> var baseCultureId = '&lt;%=BaseCultureId %&gt;';<br> var targetCultureId = '&lt;%=TargetCultureId %&gt;';<br> var pageSize = '&lt;%= GetGlobalResourceObject(&quot;mta&quot;,&quot;pageSize&quot;) %&gt;'</p> <p>if (txtSearchText[0].value != '') {<br> WebDirekt.WebServices.Translation.GetTranslation(activeDictionaryType, baseCultureId, targetCultureId, txtSearchText[0].value, 1, pageSize, jWdGetTranslationOnSuccess, jWdFail);<br> }<br> else {<br> &#36;('#list').empty();<br> &#36;(&quot;#div_ShowMsg&quot;).show();<br> }<br> return false;<br> } // jWDGetTranslation</p> <p></p> <p>function jWdGetTranslationOnSuccess(resultData) {<br> var searchResultWithNextPageNumber = resultData.split('~');<br> var nextPageNumber = searchResultWithNextPageNumber[1];<br> var searchResultData = JSON.parse(searchResultWithNextPageNumber[0]);<br> var recoudCount = searchResultData.length;</p> <p>/* Loop on searchResultData and append to list ul */<br> if (recoudCount &gt; 0) {<br> for (var i = 0; i &lt; recoudCount; i&#43;&#43;) {<br> &#36;('#list').append('&lt;li&gt; ' &#43; searchResultData[i].Item &#43; ' &lt;/li&gt;');<br> }<br> &#36;('#list').listview('refresh');<br> }<br> // IF not searchResultData then show data not found message<br> else {<br> &#36;(&quot;#div_ShowMsg&quot;).show();<br> }</p> <p>/* If next page number is 0 then hide the Show more button */<br> if (nextPageNumber &gt; 1) {<br> /*Create the dynamic button and add to div of show more */<br> var btn = &quot;&lt;a onclick='return jWdShowNextPage(&quot;<br> btn &#43;= nextPageNumber;<br> btn &#43;= &quot;);' class='showMore showMore-corner-all showMore-shadow' title=&quot;;<br> btn &#43;= '&lt;%= GetLocalResourceObject(&quot;btnShowMoreToolTip&quot;) %&gt;&gt;';<br> btn &#43;= '&lt;%=GetLocalResourceObject(&quot;btnShowMoreText&quot;) %&gt;';<br> btn &#43;= &quot;&lt;/a&gt;&quot;;<br> &#36;(&quot;#showMore&quot;).html(btn);<br> &#36;(&quot;#showMore&quot;).show();<br> }<br> else {<br> &#36;(&quot;#showMore&quot;).hide();<br> }<br> } // jWdGetTranslationOnSuccess</p> <p>function jWdFail(resultError) {<br> alert(resultError);<br> }</p> <p>function jWdShowNextPage(nextPageNumber) {<br> &#36;(&quot;#div_ShowMsg&quot;).hide();<br> var txtSearchText = &#36;(&quot;#&lt;%= txtSearchText.ClientID %&gt;&quot;)<br> var activeDictionaryType = '&lt;%=ActiveDictionaryType %&gt;';<br> var baseCultureId = '&lt;%=BaseCultureId %&gt;';<br> var targetCultureId = '&lt;%=TargetCultureId %&gt;';<br> var pageSize = '&lt;%= GetGlobalResourceObject(&quot;mta&quot;,&quot;pageSize&quot;) %&gt;'</p> <p>// Call the web service<br> WebDirekt.WebServices.Translation.GetTranslation(activeDictionaryType, baseCultureId, targetCultureId, txtSearchText[0].value, nextPageNumber, pageSize, jWdGetTranslationOnSuccess, jWdFail);<br> return false;<br> } //jWdShowNextPage</p> <p></p> <p>&nbsp;&lt;asp:TextBox ID=&quot;txtSearchText&quot; runat=&quot;server&quot; onblur=&quot;trim(this);&quot; MaxLength=&quot;256&quot;&gt;&lt;/asp:TextBox&gt;</p> <p>&lt;ul id=&quot;list&quot; data-role=&quot;listview&quot; data-inset=&quot;true&quot;&gt;&lt;/ul&gt;</p> <p>&lt;div id=&quot;showMore&quot;&gt;<br> &lt;/div&gt;</p> <p>&lt;asp:LinkButton ID=&quot;lnkBtnSearch&quot; runat=&quot;server&quot; data-transition=&quot;slide&quot; data-theme=&quot;a&quot;&nbsp;meta:resourcekey=&quot;lnkBtnSearch&quot; data-icon=&quot;arrow-r&quot; data-role=&quot;button&quot; data-iconpos=&quot;right&quot; OnClientClick=&quot;return jWDGetTranslation();&quot; CssClass=&quot;lnkBtnCss&quot; /&gt;</p> <p></p> <p>When &nbsp;i enter the text and enter keyboard button twice or more then it call web service twice or more&nbsp;</p> <p>so how i prevent the calling web service multiple time when i enter the my &nbsp;default search button&nbsp;</p> <p></p> <p></p> 2012-04-03T06:16:57-04:004914952http://forums.asp.net/p/1788434/4914952.aspx/1?Re+Calling+Web+service+twice+or+more+when+enter+keyword+button+twice+or+more+Re: Calling Web service twice or more when enter keyword button twice or more <p>Hi a.hole,</p> <p>So based on your description, I understand that you use JQuery to build a webform which allow user to search some data by clicking a button. And in the button_click script event, you will invoke a function which makes AJAX call (against a webservice method) to retrieve search result. And you want to prevent the AJAX search function call be invoked multiple times in case user click the button multiple times,correct?</p> <p>If so, I think the most straightforward and reliable means is to add some flag which marks if the search function has been invoked annd not finished yet. And in your script click event function, you will check this flag variable to determine if you will execute the AJAX call or not. For example:</p> <pre class="prettyprint">var is_search_in_progress = false; $('btnSearch').click(function(){ if(is_search_in_progress == true) return; is_search_in_progress = true; // make the ajax call here });</pre> <p>&nbsp;</p> <p>and don't forget the reset the flag after the AJAX function call(your search webmethod call) finishes. And you should use the &quot;complete&quot; event so that it will be called no matter the call succeed or failed.</p> <p>BTW, the above example use a global script variable to hold the flag, you can also use the JQuery data API to store the flag in the associated data bag of the search button element.</p> <p>#.data() <br> <a href="http://api.jquery.com/data/">http://api.jquery.com/data/</a></p> <p>&nbsp;</p> <p>&nbsp;</p> 2012-04-04T03:19:58-04:00