Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Ajax jQuery
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"
type = "text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"
type = "text/javascript"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css"
rel = "Stylesheet" type="text/css" />
<script type="text/javascript"> 
   // if you use jQuery, you can load them when dom is read.
   $(document).ready(function () {
       var prm = Sys.WebForms.PageRequestManager.getInstance();    
       prm.add_initializeRequest(InitializeRequest);
       prm.add_endRequest(EndRequest);
 
       // Place here the first init of the autocomplete
       InitAutoCompl();
    });        
 
    function InitializeRequest(sender, args) {
    }
 
    function EndRequest(sender, args) {
       // after update occur on UpdatePanel re-init the Autocomplete
       InitAutoCompl();
    }
 
   function InitAutoCompl() {
       $("#txtSearch").autocomplete({
           source: function (request, response) {
               $.ajax({
                   url: '<%=ResolveUrl("~/Service.asmx/GetCustomers") %>',
                   data: "{ 'prefix': '" + request.term + "'}",
                   dataType: "json",
                   type: "POST",
                   contentType: "application/json; charset=utf-8",
                   multiple: true,
                   width: 400,
                   minChars: 2,
                   cacheLength: 1,
                   multipleSeparator: " ",
                   success: function (data) {
                       response($.map(data.d, function (item) {
 
                           return {
                               label: item.split('-')[0],
                               val: item.split('-')[1]
                           }
                       }))
                   },
                   error: function (response) {
                       alert(response.responseText);
                   },
                   failure: function (response) {
                       alert(response.responseText);
                   }
               });
           },
           select: function (e, i) {
               $("#<%=hfCustomerId.ClientID %>").val(i.item.val);
               var origEvent = event;
               while (origEvent.originalEvent !== undefined)
                   origEvent = origEvent.originalEvent;
               if (origEvent.type == 'keydown')
                   $("#bttnSearch").click();
               return true;  
 
           },
 
           minLength: 1
       });
      
  }    
  </script>
 
this is what i got till now. What is my problem is
1) when i hover over the popup lists using keyboard navigation key it displays items in text box, but when I use mouse it doesn't display in textbox.(?).
2) when i select the list item it gets selected in text box but does not fire text box textchange event.(?).
 

my aspx
 
<asp:UpdatePanel ID="up" runat="server"><contenttemplate>
     <asp:HiddenField ID="hfCustomerId" runat="server" />
<asp:Label ID="lblEmpCodeSrch" runat="server" Text="EmpCode" CssClass="label">   
 
  <asp:TextBox ID="txtSearch" runat="server" Width="815px" ToolTip="Enter Employeecode" 
            ontextchanged="txtSearch_TextChanged">
     <asp:Button ID="bttnSearch" runat="server" CssClass="submit" Height="23px" Text="Search" onclick="bttnSearch_Click" />
 </contenttemplate>
Here I tried to explain my question properly. Any help appreciated.
Posted 20-Mar-13 23:44pm
v2
Comments
Sukanta Dey at 8-May-13 5:40am
   
May be you can try the code given below:
 
$("#txtSearch").live("blur", function (event) {
var autocomplete = $(this).data("autocomplete");
var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex($(this).val()) + "$", "i");
var myInput = $(this);
autocomplete.widget().children(".ui-menu-item").each(function () {
//Check if each autocomplete item is a case-insensitive match on the input
var item = $(this).data("item.autocomplete");
if (matcher.test(item.label || item.value || item)) {
//There was a match, lets stop checking
autocomplete.selectedItem = item;
return;
}
});
//if there was a match trigger the select event on that match
//I would recommend matching the label to the input in the select event
if (autocomplete.selectedItem) {
autocomplete._trigger("select", event, {
item: autocomplete.selectedItem
 
});
//Do something here
 
//there was no match, clear the input
} else {
$(this).val('');
selectedManagerId = 0;
}
});

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

$("#txtSearch").live("blur", function (event) {
            var autocomplete = $(this).data("autocomplete");
            var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex($(this).val()) + "$", "i");
            var myInput = $(this);
            autocomplete.widget().children(".ui-menu-item").each(function () {
                //Check if each autocomplete item is a case-insensitive match on the input
                var item = $(this).data("item.autocomplete");
                if (matcher.test(item.label || item.value || item)) {
                    //There was a match, lets stop checking
                    autocomplete.selectedItem = item;
                    return;
                }
            });
            //if there was a match trigger the select event on that match
            //I would recommend matching the label to the input in the select event
            if (autocomplete.selectedItem) {
                autocomplete._trigger("select", event, {
                    item: autocomplete.selectedItem
 
                });
        //Do something here

                //there was no match, clear the input
            } else {
                $(this).val('');
                selectedManagerId = 0;
            }
        });
  Permalink  
Comments
komal Singh Baghel at 8-May-13 7:44am
   
Thanks a lot for long waiting answer.Thanks thanks a lot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Marcin Kozub 275
1 OriginalGriff 249
2 Sergey Alexandrovich Kryukov 215
3 Praneet Nadkar 197
4 Richard MacCutchan 182
0 OriginalGriff 8,048
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 8 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100