concept FormattingBehavior in category .net

appears as: The FormattingBehavior, FormattingBehavior
ASP.NET AJAX in Action

This is an excerpt from Manning's book ASP.NET AJAX in Action.

Figure 8.9. The FormattingBehavior lets you implement the in-place-edit functionality in a web form. By using CSS, you can simulate a visual effect that turns a label into a text field.
Listing 8.7. Simulating the in-place edit functionality with the FormattingBehavior
<div class="form"> <div> <span>Name:</span> <asp:TextBox ID="Name" runat="server"></asp:TextBox> </div> <div> <span>Last Name:</span> <asp:TextBox ID="LastName" runat="server"></asp:TextBox> </div> </div> <script type="text/javascript"> <!-- Sys.Application.add_init(pageInit); function pageInit(sender, e) { $create(Samples.FormattingBehavior, { 'hoverCssClass':'field_hover', 'focusCssClass':'field_focus' }, {}, {}, $get('Name')); $create(Samples.FormattingBehavior, { 'hoverCssClass':'field_hover', 'focusCssClass':'field_focus' }, {}, {}, $get('LastName')); } //--> </script>
Figure 8.9. The FormattingBehavior lets you implement the in-place-edit functionality in a web form. By using CSS, you can simulate a visual effect that turns a label into a text field.

Based on what you’ve learned in the previous sections, your mission is to encapsulate the client logic into a behavior. In the class’s prototype, you handle the behavior’s lifecycle as well as the events raised by the associated DOM element. The complete code for the FormattingBehavior behavior is shown in listing 8.6.

Listing 8.6. Code for the FormattingBehavior class

To understand how it works, let’s take a $create statement and see how you can generate the same statement using a script descriptor. The $create statement that you used in section 8.3.2 to create an instance of the FormattingBehavior behavior is perfect:

9.3.3. An extender for FormattingBehavior

In chapter 8, we demonstrated how to enrich a text box element by simulating in-place edit functionality with the help of a client behavior. Now that you’ve implemented this client component, it would be great if you could wire it to TextBox 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.