MSDN Magazine - February 2008 - (Page 45) not included in the Text property, even though it is shown to the user in the page. So how can you parse the value returned by Text into the logical data type—be it a date or a decimal? You can use the static Parse method on the DateTime and Decimal types. But you must pay attention to the culture you use. For example, “02/04/2007” can be either the fourth of February (U.S. culture) or the second of April (European culture). The fact is that there’s no guaranteed matching between the culture used by the input page and the server page. There’s a risk that the user will type the date according to the European culture and have it processed as U.S. culture data. Worse yet, the 1200 value entered using, say, Italian decimal and thousand separators in a numeric textbox may cause an exception to be thrown because the parser of the Decimal type defaults to the U.S. culture. Let’s see how to work around these issues. The key fact to remember is that extenders default to the en-US culture unless the CultureName property is explicitly set. On the server, the system defaults to the value of the UICulture property on the Page class. In your codebehind class, you first obtain a CultureInfo object that reflects the culture used for the user interface. You can proceed as shown here: string culture = “en-us”; if (!String.IsNullOrEmpty(MaskedEditExtender1.CultureName)) culture = MaskedEditExtender1.CultureName; CultureInfo info = new CultureInfo(culture); responsible for all the logic that provides suggestions to the user. The extender creates a dropdown list-style panel and positions it right at the bottom of the textbox. You can style and animate the panel to your liking. Here’s the code to associate an autocomplete extender with a textbox: Next, you call the Parse method specifying a format provider based on the selected culture: NumberFormatInfo numberInfo = info.NumberFormat; DateTimeFormatInfo dateInfo = info.DateTimeFormat; DateTime when = DateTime.Parse(TextBox1.Text, dateInfo); decimal amount = Decimal.Parse(TextBox2.Text, numberInfo); Figure 3 shows the behavior of the same page when using different cultures for input. The extender is bound to a Web service that provides the words to populate the list. The MinimumPrefixLength property instructs the control about when to place a call to the Web service. The text already typed in will be used as input for the specified Web service method. The response is used to populate the dropdown list. Caching may be turned on, too. In this way, typing the same prefix more than once results in a single call to the Web service. Furthermore, depending on the way the Web service retrieves its data, you can also enable caching on the server and save some extra round-trips to a database or another remote data store. The full list of properties supported by the AutoComplete extender can be found in Figure C at go.microsoft.com/fwlink/?LinkId=105572. It should be noted that other extenders have properties in addition to those you’ll find listed there. There is TargetControlID, which gets and sets the ID of the extended control, and Enabled, which allows the extender’s capabilities to be turned on and off programmatically and is set to true by default. There’s also the BehaviorID property, which sets the name of the client-side JavaScript object that provides the extended behavior. Finally, there are two more properties of interest—ClientState and EnableClientState. ClientState is a string property that holds the client state of the extender. The state is persisted using a hidden field whose name can be set through Textbox Autocompletion Autocompletion is a feature you’re undoubtedly familiar with. It predicts the word the user is typing based on the first few characters that are entered. Internet Explorer keeps track of all that’s been typed into the address bar and form fields to populate autocomplete. Of course, this feature is entirely browser-based and can be turned on and off for and tags by setting the autocomplete attribute to off. The autocomplete attribute is not a standard HTML attribute, but today nearly all browsers support it. The AutoComplete extender in the AJAX Control Toolbox provides the same behavior for textbox controls, but it makes the developer Figure 3 Parsing Data Back to .NET Types Using Different Cultures Cutting Edge february2008 45 http://go.microsoft.com/fwlink/?LinkId=105572
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.