1. Trang chủ
  2. » Công Nghệ Thông Tin

Pro Server Controls and AJAX Components phần 8 pot

77 346 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 77
Dung lượng 2,38 MB

Nội dung

514 CHAPTER 10 ■ OTHER SERVER CONTROLS get { object size = ViewState["size"]; if (size == null) return 0; else return (int)size; } set { ViewState["size"] = value; } } public bool Password { get { object password = ViewState["password"]; if (password == null) return false; else return (bool)password; } set { ViewState["password"] = value; } } public bool Numeric { get { object numeric = ViewState["numeric"]; if (numeric == null) return false; else return (bool)numeric; } set { ViewState["numeric"] = value; } } Cameron_865-2C10.fm Page 514 Thursday, February 21, 2008 1:01 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com CHAPTER 10 ■ OTHER SERVER CONTROLS 515 public event EventHandler TextChanged; public bool LoadPostData(string postDataKey, NameValueCollection postCollection) { string postedValue = postCollection[postDataKey]; if (!Text.Equals(postedValue)) { Text = postedValue; return true; } else return false; } public void RaisePostDataChangedEvent() { OnTextChanged(EventArgs.Empty); } protected virtual void OnTextChanged(EventArgs e) { if (TextChanged != null) TextChanged(this, e); } } } As we discussed previously, mobile controls do not render themselves. Instead, mobile controls are associated with device adapters to handle rendering tasks. In our example, we implement two device adapters: one for HTML devices and the other for WML devices. The HTML Device Adapter This device adapter is pretty easy to create, because the rendering code is essentially the same as the sample server control in Chapter 5. Device adapters follow a naming convention of RenderingTechnologyControlNameAdapter, which translates to HtmlMCTextBoxAdapter for our sample. Listing 10-14 contains the source code for the HTML device adapter. Listing 10-14. The HtmlMCTextBoxAdapter Source File using System.Web.UI.MobileControls.Adapters; namespace ControlsBook2Lib.Ch10.Adapters { public class HtmlMCTextBoxAdapter : HtmlControlAdapter { protected new MCTextBox Control Cameron_865-2C10.fm Page 515 Thursday, February 21, 2008 1:01 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 516 CHAPTER 10 ■ OTHER SERVER CONTROLS { get { return (MCTextBox)base.Control; } } public override void Render(HtmlMobileTextWriter writer) { // write out the HTML tag writer.Write("<input name=\"" + Control.UniqueID + "\" "); writer.Write("value=\"" + Control.Text + "\" "); if (Control.Password) { writer.Write("type=\"password\" "); } if (Control.Size != 0) { writer.Write("size=\"" + Control.Size + "\" "); } writer.Write("/>"); if (Control.BreakAfter) { writer.Write("<br>"); } } } } The device adapter inherits from HtmlControlAdapter and implements two methods. We replace the Control property using the new keyword with a strongly typed Control read-only property. ASP.NET populates this property with the associated MCTextBox control at runtime. Render is the other method we implement, and it has a few enhancements when compared to the original rendering code from the TextBox control in Chapter 5. We have logic to add a <br> tag if the BreakAfter property has a value of true. Similarly, we render the input tag as of type password if the Password property is set to true. We also set the size for the <input> tag. The Size property does not enforce a rule, but it does set the initial width in characters for the control. Following the convention for the mobile control TextBox, we ignore the Numeric and Title properties’ settings when rendering HTML. One difference from the rendering logic in Chapter 5 is that instead of using the this reference, we use the strongly typed reference stored in the Control property to get control data for rendering. Also, the writer parameter is a reference to HtmlMobileTextWriter to handle markup output. Cameron_865-2C10.fm Page 516 Thursday, February 21, 2008 1:01 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com CHAPTER 10 ■ OTHER SERVER CONTROLS 517 The WML Device Adapter The WML device adapter is nearly identical to the HTML device adapter, except, of course, for the Render method. Listing 10-15 presents the code for WmlMCTextBoxAdapter. Listing 10-15. The WmlMCTextBoxAdapter.cs Source File using System.Web.UI.MobileControls.Adapters; namespace ControlsBook2Lib.Ch10.Adapters { public class WmlMCTextBoxAdapter : WmlControlAdapter { protected new MCTextBox Control { get { return (MCTextBox)base.Control; } } public override void Render(WmlMobileTextWriter writer) { string Format; if (Control.Numeric) { Format = "*N"; //Set format to any number of numeric characters } else { Format = "*M"; //Set format to any number of characters } writer.RenderTextBox(Control.UniqueID, Control.Text, Format, Control.Title, Control.Password, Control.Size, Control.MaxLength, false, Control.BreakAfter); } } } The Render method takes advantage of a method on the WmlMobileTextWriter writer named RenderTextBox. This method takes a series of parameters for customizing the output. Table 10-15 lists the parameters for RenderTextBox. When you compare the parameters of this method with the properties on the mobile TextBox and MCTextBox controls, you can see that the Numeric and Title properties are geared toward WML-capable devices. Cameron_865-2C10.fm Page 517 Thursday, February 21, 2008 1:01 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 518 CHAPTER 10 ■ OTHER SERVER CONTROLS The other logic in the Render method for the WmlMCTextBoxAdapter class modifies the format parameter for this method, setting it to *N for unlimited numeric characters or *M for unlimited any type of characters. These settings come from the WML specification for the <input> tag. Table 10-16 details the available values for the format setting. Creating device adapters requires a deep understanding of the nuances of the markup language. (Either that or a good reference close at hand!) Table 10-15. WmlMobileTextWriter.RenderTextBox Parameters Parameter Description breakAfter Indicates whether a <br> tag should be rendered after the <input> tag format Permits application of WML-specific formatting options generateRandomID Indicates whether the identifier for the control should be encrypted id Identifier of the associated mobile control maxLength Stores the maximum length permitted for the string password Indicates if the data should be masked with the password character * size Stores the size of the string title Stores the title for the text box value Value to initialize the control Table 10-16. Permitted Settings for the WML <input> Tag format Value Format Description A Punctuation or uppercase alphabetic characters. a Punctuation or lowercase alphabetic characters. M All characters permitted. m All characters permitted. N Numeric characters only. X Uppercase characters only. x Lowercase characters only. nf n indicates a number between 1 and 9 for the number of characters permitted. Replace f with one of the preceding letters to specify what characters are legal. *f * indicates any number of characters permitted. Replace f with one of the preceding letters to specify what characters are legal. Cameron_865-2C10.fm Page 518 Thursday, February 21, 2008 1:01 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com CHAPTER 10 ■ OTHER SERVER CONTROLS 519 Mapping Device Adapters Now that we have created our mobile server control and device adapters, it is time to modify a configuration file so that the ASP.NET runtime can select the correct device adapter to render the mobile control. We can inherit from the machine.config file and create a new device mapping in the web.config file, or we can modify the machine.config file. The section of the configuration file we need to customize is the <mobileControls> element. For a given device target, we need to map the mobile control to a device adapter. Here is the syntax to map a mobile server control to a device adapter: <control name= "controlName, assembly" adapter="adapterName, assembly" /> If the assembly is registered in the GAC, you can omit the assembly name. For our sample, we chose to modify the <mobileControls> tag in web.config by adding the following section: <mobileControls cookielessDataDictionaryType= "System.Web.Mobile.CookielessData"> <device name="ControlsBookHtml" inheritsFrom="HtmlDeviceAdapters"> <control name= "ControlsBookLib.Ch11.MCTextBox,ControlsBookLib" adapter= "ControlsBookLib.Ch11.Adapters.HtmlMCTextBoxAdapter,ControlsBookLib" /> </device> <device name="ControlsBookWml" inheritsFrom="WmlDeviceAdapters"> <control name= "ControlsBookLib.Ch11.MCTextBox,ControlsBookLib" adapter= "ControlsBookLib.Ch11.Adapters.WmlMCTextBoxAdapter,ControlsBookLib" /> </device> </mobileControls> In the preceding section, we inherit from the standard device mappings listed in machine.config and can simply make the modifications we need for our control. This method makes it easy to add server controls to device adapter mappings. Testing MCTextBox Now that we have everything set up, we can put our new control through its paces. The sample mobile page is very similar to the sample in Chapter 5. Because the new control keeps the code the same as much as possible, it handles postback data and generates server-side events if the data changes in the MCTextBox. Listings 10-16 and 10-17 provide the source for MCTextBoxDemo.aspx and its code-behind file. Listing 10-16. The MCTextBoxDemo.aspx File <%@ Page Language="c#" CodeBehind="MCTextBoxDemo.aspx.cs" Inherits="ControlsBook2Mobile.Ch10.MCTextBox" AutoEventWireup="True" %> Cameron_865-2C10.fm Page 519 Thursday, February 21, 2008 1:01 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 520 CHAPTER 10 ■ OTHER SERVER CONTROLS <%@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls" Assembly="System.Web.Mobile" %> <%@ Register TagPrefix="ApressMC" Namespace="ControlsBook2Lib.Ch10" Assembly="ControlsBook2Lib" %> <head> <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"> <meta content="C#" name="CODE_LANGUAGE"> <meta content="http://schemas.microsoft.com/Mobile/Page" name="vs_targetSchema"> </head> <body> <mobile:Form ID="Form1" Runat="server"> <mobile:Label ID="Label1" Runat="server">Change the value:</mobile:Label> <ApressMC:MCTextBox ID="MCTextBox1" runat="server" Text="Hi There!" MaxLength="15" Numeric="False" Password="False" Size="10" OnTextChanged="MCTextBox1_TextChanged"> </ApressMC:MCTextBox> <mobile:Command ID="Command1" Runat="server">Command</mobile:Command> <mobile:Label ID="ChangeLabel" Runat="server">Message</mobile:Label> </mobile:Form> </body> Listing 10-17. The MCTextBoxDemo.aspx File using System; namespace ControlsBook2Mobile.Ch10 { public partial class MCTextBox : System.Web.UI.MobileControls.MobilePage { protected System.Web.UI.MobileControls.TextBox TextBox1; protected void Page_Load(object sender, System.EventArgs e) { ChangeLabel.Text = DateTime.Now.ToLongTimeString() + ": MCTextBox No change."; } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } Cameron_865-2C10.fm Page 520 Thursday, February 21, 2008 1:01 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com CHAPTER 10 ■ OTHER SERVER CONTROLS 521 /// <summary> // /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { } #endregion protected void MCTextBox1_TextChanged(object sender, System.EventArgs e) { ChangeLabel.Text = DateTime.Now.ToLongTimeString() + ": MCTextbox Changed! " + MCTextBox1.Text; } } } The sample consists of MCTextBox, a Command button, and a Label to display a message. When the form first appears, the message label displays “No change”. Change the value in the MCTextBox control, and click the Command button. When the mobile form reloads after the postback process, the message label displays an updated time and a message stating that the value changed. This shows that the server-side event process is correctly implemented. The MCTextBox control will render correctly using both Pocket Internet Explorer and a WML emulator, demonstrating the extensibility of the ASP.NET Framework mobile server control architecture. This concludes our discussion of mobile control development. Summary In this chapter, we provided a quick overview of the web part infrastructure. Next, we created the WebPartPageController server control to provide a UI to the end user for manipulating the web part infrastructure. After that, we created two basic server controls and migrated them to web parts as a way to demonstrate the tasks that are typically required. We then walked through enabling web part connections between the web parts. The next section began a discussion of adaptive control programming by customizing the HTML output of a server control without actually inheriting from the control. We walked through creating a simple control adapter to demonstrate how it works. The best examples of this are the DHTML control adapters available from Microsoft for download. For device-specific programming, we covered the extensibility hooks for mobile server controls, which are the StyleSheet control, templates, device-specific UI choices, user controls, custom controls, and device adapters. ASP.NET provides three default StyleSheet Style attributes in the StyleReference property: error, subcommand, and title. Cameron_865-2C10.fm Page 521 Thursday, February 21, 2008 1:01 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 522 CHAPTER 10 ■ OTHER SERVER CONTROLS Mobile control technology includes user controls, composite controls, and custom-developed controls inherited from MobileControl. Creating custom mobile server controls is very similar to creating traditional mobile controls. The two major differences are ViewState management and the rendering process. Mobile server controls that inherit from MobileControl do not render themselves but instead rely on device adapters to handle their rendering. Device adapters render mobile controls on specific mobile devices, providing support for HTML, WML, cHTML, and XHTML. MobileCapabilties inherits from HttpBrowserCapabilities and aids in detecting the closest match of what device is currently browsing a web application. Cameron_865-2C10.fm Page 522 Thursday, February 21, 2008 1:01 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 523 ■ ■ ■ CHAPTER 11 Design-Time Support Design-time support refers to working with server controls within the Visual Studio develop- ment environment. Dragging controls onto the web page Component Designer surface from the Toolbox tool window, editing server control properties in the Properties tool window, and right-clicking a control to bring up a context menu are all examples of design-time support. All these capabilities and more are made available to server control developers by the .NET Framework. In this chapter, we explore the design-time capabilities and techniques available in the .NET Framework for inclusion in custom-developed server control development efforts. Professional Quality Support for visual controls in rapid application development (RAD) environments on the Windows platform have existed since the early days of Visual Basic. As opposed to just working with a class in code, controls enhance the development environment experience and speed up devel- opment time. The qualities associated with a professional control include the following: • Ease of installation • High level of documentation • Sample code that demonstrates control functionality •Design-time support In the remainder of this book, we aim to provide you with the requisite knowledge to assist you in developing professional quality controls. In this chapter we cover design-time support. We cover localization, help file integration, and deployment in the following chapters. In the next section, we take a look at the design-time architecture provided by the .NET Framework. Design-Time Architecture The .NET Framework provides design-time customizations for both Windows controls and web controls. The customizations available in each environment differ mostly as a result of rendering technology: ASP.NET server controls generate HTML; Windows Forms controls render using GDI+; and Windows Presentation Foundation controls render in DirectX 3D. This chapter focuses on design-time capabilities for web controls, but many of the concepts discussed here apply to the Windows Forms or Windows Presentation Foundation environment as well. Cameron_865-2C11.fm Page 523 Thursday, February 21, 2008 2:19 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... that inherits from System.ComponentModel.ExpandableObjectConverter to provide expand/collapse functionality in the property browser for complex types such as properties with subproperties This also provides a more useful value for the complex type in the property browser UI Type Editors UI type editors provide a pop-up UI for editing properties listed in the Properties window An example is the Color... Cameron _86 5-2C11.fm and Thursday, February 21, 20 08 2:19 PM 530 CH APT ER 11 ■ D ESI GN-T IM E SU PPO RT public ImageMetaData ImageInfo { get { EnsureChildControls(); if (metaData == null) { metaData = new ImageMetaData(); } return metaData; } } public override ControlCollection Controls { get { EnsureChildControls(); return base .Controls; } } protected override void CreateChildControls() { Controls. Clear();... http://www.simpopdf.com Cameron _86 5-2C11.fm and Thursday, February 21, 20 08 2:19 PM CH A PT ER 11 ■ D ES IGN -T I ME S UPP O RT HtmlControlDesigner provides basic designer functionality for server controls The class that developers extend when building custom designer classes for ASP.NET server controls is System.Web.UI.Design.ControlDesigner Though Windows Forms, Windows Presentation, and web forms share a... Description IDesignerHost Used to add and retrieve services available in the design-time environment and handle events related to designer state It provides support for detecting that a designer is loading and helps manage component and designer transactions IDesignerOptionService Permits a designer to get and set property values displayed in the Windows Forms Designer property grid displayed when Tools... time IHelpService Permits a designer to create and remove help service contexts and attributes, and display help topics by keyword and URL IInheritanceService Permits a designer to search for components of derived classes and identify any inheritance attributes for each IMenuCommandService Permits a designer to search for, add, remove, and invoke menu commands at design time IReferenceService Permits... TagPrefix="apress" Namespace="ControlsBook2Lib.Ch11" Assembly="ControlsBook2Lib" %> 11   . Merge and Split Unregistered Version - http://www.simpopdf.com 522 CHAPTER 10 ■ OTHER SERVER CONTROLS Mobile control technology includes user controls, composite controls, and custom-developed controls. custom mobile server controls is very similar to creating traditional mobile controls. The two major differences are ViewState management and the rendering process. Mobile server controls that. add to the set of properties displayed in the property browser and filter the properties. Cameron _86 5-2C11.fm Page 524 Thursday, February 21, 20 08 2:19 PM Simpo PDF Merge and Split Unregistered

Ngày đăng: 12/08/2014, 23:20