Building Ajax-enabled controls in VS .NET

Print PDF-417 2d barcode in VS .NET Building Ajax-enabled controls

Building Ajax-enabled controls
PDF 417 Maker In VS .NET
Using Barcode encoder for ASP.NET Control to generate, create PDF 417 image in ASP.NET applications.
www.OnBarcode.com
EAN128 Drawer In .NET
Using Barcode printer for ASP.NET Control to generate, create EAN / UCC - 13 image in ASP.NET applications.
www.OnBarcode.com
controls in different web applications. If your intention is to not write one more line of JavaScript code or change any web controls declared in a form, building an extender is the right path. If you have the code for the FormattingBehavior class stored in a JavaScript file you ve completed the first phase of the design strategy and can move to the second phase. Mapping client properties to server properties Once the client functionality is encapsulated in a client component, you need to filter the client properties you want to configure on the server side. The goal is to create corresponding properties in the extender class and use them to set the value of the client properties. How is this possible By using a script descriptor. Recall from chapter 8 that the FormattingBehavior class exposes two properties called hoverCssClass and focusCssClass. They hold the names of the CSS classes used by the client behavior. To set their values from the server side, you need to expose corresponding properties in the extender. In preparation, it s useful to draw a table that shows the mapping between properties of the client component and properties of the extender; see table 9.1.
Encode Denso QR Bar Code In VS .NET
Using Barcode generation for ASP.NET Control to generate, create QR Code JIS X 0510 image in ASP.NET applications.
www.OnBarcode.com
Make PDF-417 2d Barcode In .NET Framework
Using Barcode printer for ASP.NET Control to generate, create PDF 417 image in ASP.NET applications.
www.OnBarcode.com
Table 9.1 Mappings between client properties and extender properties Client property Extender property
EAN13 Generation In VS .NET
Using Barcode generator for ASP.NET Control to generate, create GTIN - 13 image in ASP.NET applications.
www.OnBarcode.com
ANSI/AIM Code 128 Encoder In .NET Framework
Using Barcode generator for ASP.NET Control to generate, create Code 128C image in ASP.NET applications.
www.OnBarcode.com
hoverCssClass focusCssClass
Matrix Generation In .NET Framework
Using Barcode printer for ASP.NET Control to generate, create Matrix Barcode image in ASP.NET applications.
www.OnBarcode.com
Print UPC-E Supplement 2 In .NET
Using Barcode encoder for ASP.NET Control to generate, create Universal Product Code version E image in ASP.NET applications.
www.OnBarcode.com
HoverCssClass FocusCssClass
PDF-417 2d Barcode Decoder In VB.NET
Using Barcode reader for .NET framework Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Painting PDF-417 2d Barcode In None
Using Barcode encoder for Font Control to generate, create PDF 417 image in Font applications.
www.OnBarcode.com
Once you ve drawn the table, you re ready to move to the third and final, where you ll create the extender class and implement the server-side logic. Creating the extender An extender is a class that inherits from the base System.Web.UI.ExtenderControl class. Usually, an extender includes a group of server properties and the overrides of the methods defined in the IExtenderControl interface. Other than these, an extender shouldn t perform any tasks. Because the purpose of an extender is to provide script descriptors and script references, all the other logic added to the extender should relate to the configuration of the associated client component. Let s return to the example. The extender class is called FormattingExtender, and its code is shown in listing 9.3.
Reading Barcode In Visual C#
Using Barcode Control SDK for VS .NET Control to generate, create, read, scan barcode image in .NET framework applications.
www.OnBarcode.com
Matrix 2D Barcode Maker In .NET Framework
Using Barcode creation for VS .NET Control to generate, create Matrix image in VS .NET applications.
www.OnBarcode.com
Extenders
Code 39 Extended Drawer In None
Using Barcode creation for Font Control to generate, create Code39 image in Font applications.
www.OnBarcode.com
UPC-A Supplement 2 Decoder In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Listing 9.3 Code for the FormattingExtender class
Barcode Maker In None
Using Barcode printer for Word Control to generate, create Barcode image in Microsoft Word applications.
www.OnBarcode.com
Make UPC-A Supplement 5 In None
Using Barcode creation for Software Control to generate, create GS1 - 12 image in Software applications.
www.OnBarcode.com
using using using using using System; System.Collections.Generic; System.Web; System.Web.UI; System.Web.UI.WebControls;
QR Code JIS X 0510 Creation In None
Using Barcode drawer for Office Word Control to generate, create QR image in Word applications.
www.OnBarcode.com
DataMatrix Generation In Visual Basic .NET
Using Barcode encoder for Visual Studio .NET Control to generate, create Data Matrix ECC200 image in VS .NET applications.
www.OnBarcode.com
[TargetControlType(typeof(TextBox))] public class FormattingExtender : ExtenderControl { public string HoverCssClass { get { return (string)ViewState["HoverCssClass"]; } set { ViewState["HoverCssClass"] = value; } } public string FocusCssClass { get { return (string)ViewState["FocusCssClass"]; } set { ViewState["FocusCssClass"] = value; } } public string ScriptPath { get { return (string)ViewState["ScriptPath"]; } set { ViewState["ScriptPath"] = value; } } protected override IEnumerable<ScriptDescriptor> GetScriptDescriptors(Control targetControl) { ScriptBehaviorDescriptor desc = new ScriptBehaviorDescriptor("Samples.FormattingBehavior", targetControl.ClientID); desc.AddProperty("hoverCssClass", this.HoverCssClass); desc.AddProperty("focusCssClass", this.FocusCssClass); yield return desc; }
Barcode Maker In VB.NET
Using Barcode creator for .NET Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
QR Code Generator In Java
Using Barcode encoder for Java Control to generate, create QR image in Java applications.
www.OnBarcode.com
B Properties
IExtenderControl methods
protected override IEnumerable<ScriptReference> GetScriptReferences()| { yield return new ScriptReference(Page.ResolveClientUrl(this.ScriptPath)); } }
Building Ajax-enabled controls
Above the class declaration is a TargetControlType attribute. Its goal is to put a constraint on the types of server controls that the extender can extend. Because you pass typeof(TextBox) as an argument to the attribute, only TextBox controls can be extended by the FormattingExtender. Associating the extender with a web control other than a TextBox will cause a server exception to be thrown by ASP.NET. If you pass typeof(Control), all the controls can be extended, although it doesn t make much sense given the kind of client functionality that, in this example, you ll add to the target control. The FormattingExtender class exposes a ScriptPath property B that isn t listed in table 9.1. This property specifies the location of the JavaScript file that contains the code of the FormattingBehavior behavior. The property isn t listed in the table because it s not exposed by the client component. You ll need it when you create the ScriptReference instance that you return to the ScriptManager, so it makes sense to have it in the extender control. The other two properties are those shown in table 9.1. The HoverCssClass property stores the value assigned to the hoverCssClass property of the client behavior. The same is true for the FocusCssClass property. Note that you store and retrieve all the values from the ViewState of the extender control. For the first time, you can see how the methods C defined in the IExtenderControl interface are overridden in the extender control. As expected, the GetScriptDescriptors method returns a script descriptor for the FormattingBehavior behavior. In the override, the script descriptor uses the values of the server HoverCssClass and FocusCssClass properties to build a $create statement that contains values for the client hoverCssClass and focusCssClass properties. Finally, the GetScriptReferences method returns a ScriptReference instance with the information needed to load the right JavaScript file in the page. The location of the file is configured through the ScriptPath property.
Copyright © OnBarcode.com . All rights reserved.