Search code examples
jqueryajaxeventsajaxcontroltoolkit

jQuery fire a event based on a condition?


I have this TextBox txtCustomerName, it is associated with a Ajax AutoCompleteExtender, what I am doing is at the OnClientItemSelected of this AutoCompleteExtender calling a function LoadCutomerDetails with jQuery. Something like this..

<asp:TextBox ID="txtCustomerName" runat="server" ClientIDMode="Static"></asp:TextBox>
<ajax:AutoCompleteExtender runat="server" ID="autoComplete1" TargetControlID="txtCustomerName"
ServicePath="~/AutoComplete.asmx" ServiceMethod="GetCompletionList" MinimumPrefixLength="1" CompletionInterval="10" CompletionSetCount="15" OnClientItemSelected="PopulateData">

and Populate data is defined as

 function PopulateData() {
        var data2 = $('#txtCustomerName').val().split('-')[0];
        var newData = data2.substring(0, data2.length - 1);

        $.ajax({
            url: '/AutoComplete.asmx/GetPriorityAndRemarks',
            type: 'POST',
            timeout: 5000,
            datatype: 'xml',
            cache: false,
            data: 'arg=' + newData,
            success: function (response) {
                var doc = response;
                var result = $(doc).find("string").text();
                // the values is in form of name, address, mobile, priority and remark
                var resultAry = result.split(':');
                $('#lbCustomerName').val(resultAry[0]);
                $('#lblAddress').text(resultAry[1]);
            }
        });
    };

Now, what I wanna do is associate a event handler on focusout event of jQuery and check if lbCustomerName and lblAddress have any value, if not then this is a new customer, so open a popup dialog where details can be added, and so on..

The Problem is

  1. The focusout event fires before data could be filled lbCustomerName and lblAddress (by that Populate data), so it will always be null, even if there is data. (I checked the focus out event by showing a alert, and the alert was shown before the text of label(s) were updated.)

  2. The user can also type in the textbox and not just select a item from AutoCompleteExtender's list, right? So, in that case, the PopulalteData will not be called, but focusout will be.

What I would like to do is, call that focusout event only on condition when user types into the box (that is OnClientItemSelected is not fired), because if he is not selecting from the list, its a new customer obviosly. So is there a way to do this? Or if not exactly this, then is some other way to accomplish what I am trying?


Solution

  • Couple thoughts:

    1) Can you really assume that if user isn't selecting from list they aren't in it? For example, if the user's name is Tom and they just type it in quickly without using the autosuggest.

    2) Instead of using OnClientItemSelected can u call PopulateDate via the focusout event, and then pop up the dialog if the name/address are not in resultAry inside the ajax success callback?