.net code 128 barcode Recipe: creating an autocomplete text box in .NET framework

Draw Code 128B in .NET framework Recipe: creating an autocomplete text box

Recipe: creating an autocomplete text box
Create Code-128 In .NET
Using Barcode drawer for ASP.NET Control to generate, create Code 128 image in ASP.NET applications.
www.OnBarcode.com
Linear Barcode Maker In VS .NET
Using Barcode encoder for ASP.NET Control to generate, create Linear Barcode image in ASP.NET applications.
www.OnBarcode.com
This chapter covers
Generate PDF417 In .NET Framework
Using Barcode creator for ASP.NET Control to generate, create PDF417 image in ASP.NET applications.
www.OnBarcode.com
Code 128 Drawer In .NET
Using Barcode creator for ASP.NET Control to generate, create Code-128 image in ASP.NET applications.
www.OnBarcode.com
Creating an autocomplete text box in ASP .NET MVC Using a jQuery autocomplete plug-in
Barcode Printer In Visual Studio .NET
Using Barcode generation for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
Draw EAN128 In .NET Framework
Using Barcode encoder for ASP.NET Control to generate, create EAN / UCC - 14 image in ASP.NET applications.
www.OnBarcode.com
It s not uncommon for text boxes to automatically suggest items based on what we type. The results are further filtered as we type to give us the option to select an available item with the mouse or keyboard. One of the first examples of this in the wild was Google Suggest, shown in figure 27.1. This chapter covers the mechanics of implementing autocomplete functionality using the freely available jQuery library. We ll first
Painting Barcode In .NET Framework
Using Barcode creation for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
Generate Code 93 Full ASCII In VS .NET
Using Barcode drawer for ASP.NET Control to generate, create Uniform Symbology Specification Code 93 image in ASP.NET applications.
www.OnBarcode.com
Figure 27.1 Google Suggest filters options as we type.
Creating ANSI/AIM Code 128 In Visual Studio .NET
Using Barcode generation for ASP.NET Control to generate, create Code 128B image in ASP.NET applications.
www.OnBarcode.com
Code 128 Maker In None
Using Barcode maker for Office Word Control to generate, create Code 128 Code Set C image in Office Word applications.
www.OnBarcode.com
Creating the basic autocomplete text box
Encoding Code 39 Full ASCII In Java
Using Barcode creator for Android Control to generate, create Code 39 Extended image in Android applications.
www.OnBarcode.com
2D Barcode Maker In C#.NET
Using Barcode creator for .NET framework Control to generate, create Matrix Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
describe how to structure the code necessary to enable the functionality. Then, we ll show you how to style the UI to fit within your application s look and feel.
Draw Code 3 Of 9 In VB.NET
Using Barcode generator for .NET Control to generate, create Code 3/9 image in VS .NET applications.
www.OnBarcode.com
Decoding DataMatrix In None
Using Barcode recognizer for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
27.1 Creating the basic autocomplete text box
Scanning UPC-A In None
Using Barcode decoder for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
PDF417 Recognizer In None
Using Barcode recognizer for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
A rudimentary implementation of this automatic suggestion feature would be to monitor keypresses and fire off Ajax requests for each one. Of course, this means that a fast typist would trigger many requests, most of which would be immediately discarded for the next request coming in 5 milliseconds (ms). An effective implementation would take into account a typing delay and also provide keyboard and mouse support for selecting the items. Luckily, jQuery has an extensive list of plug-ins available. One such plug-in is Dylan Verheul s autocomplete, which you can download at www.dyve.net/jquery/ along with a few others, including googlemaps and listify. Another, arguably equally popular, autocomplete plug-in is available from J rn Zaefferer at http://mng.bz/60ct. The plug-ins are similar, so although this chapter uses Dylan Verheul s autocomplete plugin, most of what you read here will apply to the other plug-in as well. The implementation of the autocomplete functionality is that we have a simple text box on our page, and the jQuery plug-in adds the behavior necessary to handle keypress events and fire the appropriate Ajax requests to a URL that will handle the requests. The URL points to a controller action, and by convention the responses are formatted so that the plug-in can handle them. Assume for our purposes that we want to filter U.S. cities in the text box. The first step is to add a controller, an action, and a view for displaying the UI for this example. Ensure that jQuery (in this case, jquery-1.4.1.js) and jquery.autcomplete.js are referenced at the top of the view (or master page):
Reading PDF-417 2d Barcode In Visual Basic .NET
Using Barcode scanner for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
UPC-A Creation In None
Using Barcode generation for Office Word Control to generate, create UPC-A image in Microsoft Word applications.
www.OnBarcode.com
<script type="text/javascript" src="../../scripts/jquery-1.4.1.js"></script> <script type="text/javascript" src="../../scripts/jquery.autocomplete.js"> </script>
Print QR Code In Visual Basic .NET
Using Barcode creator for VS .NET Control to generate, create QR image in .NET applications.
www.OnBarcode.com
Creating 1D Barcode In .NET Framework
Using Barcode encoder for VS .NET Control to generate, create 1D image in Visual Studio .NET applications.
www.OnBarcode.com
Here s how we d output the text box for city:
<%= Html.TextBox("city") %>
Package this up with a simple controller, as shown in listing 27.1.
Listing 27.1 Controller and action for displaying our test page
public class HomeController : Controller { public ActionResult Index() { return View(); } }
This is a simple action method, and it returns the default view. Figure 27.2 shows what we d expect.
Recipe: creating an autocomplete text box
Figure 27.2 text box
Our simple view with a
Now, let s add a little JavaScript to add the autocomplete behavior:
<script type="text/javascript"> $(document).ready(function() { $("input#city") .autocomplete('<%= Url.Action("Find", "Cities") %>'); }); </script>
Place the script in the <head> of the page. You can see that the URL for the autocomplete behavior is specified as Url.Action("Find", "Cities"). This will point to a Find() action on the CitiesController. We ll need to write this controller and action as shown in listing 27.2.
The autocomplete plug-in can also filter local data structures. This is useful when we have a limited set of data and we want to minimize requests sent to the server. The autocomplete plug-in in local mode is also much faster, because there s no Ajax request being made behind the scenes. The only downside is that we must render the entire array onto the view as a JavaScript array.
Listing 27.2 Action to find cities from an autocomplete Ajax request
public class CitiesController : Controller { private readonly ICityRepository _repository; public CityController() { string csvPath = System.Web.HttpContext.Current .Server.MapPath("~/App_Data/cities.csv"); _repository = new CityRepository(csvPath); } public CitiesController(ICityRepository repository) { _repository = repository; }
Loads CSV file containing cities Loads CSV into repository Defines testable constructor
Creating the basic autocomplete text box
public ActionResult Find(string q) { string[] cities = _repository.FindCities(q); return Content(string.Join("\n", cities)); } }
383 Accepts parameter q from autocomplete Returns raw text
The details of the CityRepository can be found in the code samples provided with the book. For now, we ll focus on the new Find(string q) action. Because this is a standard action, we can navigate to it in our browser and test it out. Figure 27.3 shows a quick test.
Copyright © OnBarcode.com . All rights reserved.