March 27, 2011 by Christoff Truter C# ASP.NET
Back in 2007 I had the wonderful privilege to work at Web Africa, one of the largest
internet service providers in South Africa.
One of my PHP projects (or at least a small part of the project) involved creating an autocomplete textbox when searching for
domains located within the company MySQL database.
If memory serves me correctly (it probably doesn't) that's around the
same time Google introduced Google suggests at Google labs - something that is
standard to its search these days.
(subtle attempt at humour, ahhh.... ha ha)
Last month we needed the same functionality in one of our ASP.NET projects at my current company.
In this post we're going to have a look at how to create our very own autocomplete textbox using
ASP.net (C#) (with a little bit of JavaScript of course).
Lets jump into some code...
We can retrieve our autocomplete results via PageMethods (ajax request, json response), which we
need to enable in our scriptmanager at the top of our page like seen in the following snippet:
<asp:ScriptManager runat="server" ID="scriptmanager" EnablePageMethods="true"> <Scripts> <asp:ScriptReference Path="~/js/autocomplete.js" /> </Scripts> </asp:ScriptManager>
[WebMethod] public static string[] GetList(string prefixText, int count) { // Dummy data - don't do this string[] data = new string[] { "Christoff Truter", "Gerhardt Stander", "Roland Cooper", "Alexander Mehlhorn", "Derek Campher", "Jurgens Truter", "Hanno Coetzee", "Wayne Kleynhans", "Pieter Du Plooy", "Pam Nizar" }; return (from p in data where p.IndexOf(prefixText, StringComparison.OrdinalIgnoreCase) >= 0 select p).Take<String>(count).ToArray(); }
<asp:ScriptManager runat="server" ID="scriptmanager"> <Services> <asp:ServiceReference Path="~/MyService.asmx" /> </Services> <Scripts> <asp:ScriptReference Path="~/js/autocomplete.js" /> </Scripts> </asp:ScriptManager>
using System; using System.Linq; using System.Web.Services; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.Web.Script.Services.ScriptService] public class MyService : System.Web.Services.WebService { [WebMethod] public string[] GetList(String prefixText, Int32 count) { // Dummy data - don't do this string[] data = new string[] { "Christoff Truter", "Gerhardt Stander", "Roland Cooper", "Alexander Mehlhorn", "Derek Campher", "Jurgens Truter", "Hanno Coetzee", "Wayne Kleynhans", "Pieter Du Plooy", "Pam Nizar" }; return (from p in data where p.IndexOf(prefixText, StringComparison.OrdinalIgnoreCase) >= 0 select p).Take<String>(count).ToArray(); } }
<script type="text/javascript"> function pageLoad() { AutoComplete('<%=txtTest.ClientID %>', 10, 500, PageMethods, 'GetList'); } </script> <asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
function AutoComplete(targetId, count, timeout, service, method) { var timeoutId; var request; var target = document.getElementById(targetId); var list = CreateList(target); $addHandler(target, "keyup", function() { if (timeoutId != undefined) { clearTimeout(timeoutId); } if (request != undefined) { var executor = request.get_executor(); if (executor.get_started()) { executor.abort(); } } timeoutId = setTimeout(function() { request = service._staticInstance[method](target.value, count, success, function() { }); }, timeout); });
function success(result) { list.options.length = 0; // clear listbox if (result != '') { list.setAttribute("size", result.length + 1); list.style.display = "block"; for (var index = 0; index < result.length; index++) { list.options.add(new Option(result[index], result[index], false)); } } else { list.style.display = "none"; } }
$addHandler(document, "click", function(e) { if ((e.srcElement == target) || (e.target == target)) { return false; } list.style.display = "none"; if (list.value != '') { target.value = list.value; } }); } // Append the autocomplete listbox to the target textbox function CreateList(sender) { var div = document.createElement("div"); var list = document.createElement("select"); list.style.position = "absolute"; list.style.display = "none"; div.appendChild(list); sender.parentNode.insertBefore(div, sender.nextSibling); return list; }
April 10, 2011
autocomplete suggestion box December 4, 2014 by Anonymous
hello sir, master page auto complete suggestion is possible or not? if it is possible give some examples