Client functions of ASP. NET 2.0 Server controls |
|
|
Most server controls applied in the presentation layer are mainly composed of two parts: server-side functions and client functions. Server functions are always the core of server controls. With the development of technology, client functions become more and more important. Only when the two parts work together can you create server controls with powerful functions and rich interfaces. This article will discuss how to implement client functions in server controls, including client functions overview, simple client functions, and complex client functions, and how to deploy the client file. 1. Client features In Web programming, client functions are traditionally handled by web page developers and are not encapsulated in server components. ASP. NET is out of this category and enables the Server Control to issue client scripts, so that the server control can combine client processing with server processing. The implementation of client functions is of great significance to improve the interaction and scalability of server controls. For example, common Treeview, tabstrip, and toolbar controls. These excellent server controls have strong interactivity and rich user interfaces. The implementation of these features is inseparable from client functions. The main technologies used to implement client functions are client scripts (JavaScript, VBScript, etc.) and DHTML. Therefore, as a qualified developer, you must be skilled in using these technologies. In addition, you must master the method of Closely combining client functions with server controls. These methods include implementing simple client functions, implementing complex client functions, and deploying client script files. 2. implement simple client functions If the client function of the custom Server Control is relatively simple, for example, only a window is displayed or the background color is changed, in this case, the client script file is not encapsulated, instead, it is directly implemented in the control rendering. The key to its implementation is to add appropriate client processing in the butes of the control.Program. The following example shows the control myclickbutton derived from the system. Web. UI. webcontrols. Button class, which provides an event handler for the client-side click event. See the followingCode:
Public class myclickbutton: button { // Related Code ...... Protected override void addattributestorender (htmltextwriter writer ){ Base. addattributestorender (writer ); Writer. addattrim ("onclick", "window. Confirm ('Thank you! ');"); } } |
If you have read the previousArticleSo it is easy to understand the above Code. The preceding Code overwrites the addattributestorender method. It defines an attribute named onclick for the myclickbutton Control and Its Value indicates that a confirmation window containing custom information is displayed on the client. If the client functions of the server control developed by the reader are relatively simple, you can try to rewrite the addattributestorender Method for implementation. The following is the ASPX page created to use the myclickcontrol control.Source code.
<% @ Page Language = "C #" %> <% @ Register tagprefix = "Custom" namespace = "mycontrols" assembly = "mycontrols" %> <HTML> <Body> <Form runat = Server> Click the following button. <Custom: myclickbutton id = "Demo" runat = server/> <Br> </Form> </Body> </Html> |
When you click the myclickcontrol control, a confirmation window containing the prompt is displayed immediately. Note that the pop-up Confirmation window is not because of page return, but the result that the user inspires the client processing program. After you click "OK", the page will be returned. 3. implement complex client functions If the client functions are complex, the code that completes the functions is generally encapsulated in the client script file. To closely integrate these script files with server controls, the. NET 2.0 Framework provides the necessary methods to add client script files to server controls. These methods are basically included in the clientscriptmanager class. Developers can obtain clientscriptmanager instances by calling the clientscript attribute of the page class. This class is used to manage scripts, register scripts, and add scripts to pages. Readers may be unfamiliar with the clientscriptmanager class, which is a new class in ASP. NET 2.0. This class is created to replace some of the methods of the stopped page class for script management. For example, in ASP. net 1. registerclientscriptblock, registerstartupscript, and so on. These methods are all stopped and implemented by using the clientscriptmanager class. The following lists several common methods related to implementing complex client functions from the clientscriptmanager class.
(1) registerclientscriptblock Method
Add a script block to the top of the page. Create a script as a string and pass it to the method. The method then adds the script to the page. You can use this method to insert any script into the page. Note that the script may be rendered to the page before all elements are completed. Therefore, you may not be able to reference all elements on the page from the script. (2) registerclientscriptinclude Method Similar to the registerclientscriptblock method, this method adds a script block that references an external. js file. Include files added before any other dynamically added scripts; therefore, you may not be able to reference certain elements on the page. (3) registerstartupscript Method Add a script block to the page, which is executed before the onload event of the page is triggered after the page is loaded. This script is generally not created as an event handler or function; it usually only contains the statement to be executed once. (4) registeronsubmitstatement Method The script executed by adding the onsubmit event on the response page. This script is executed before the submission page. You can cancel the submission. |
(5) isstartupscriptregistered Method
Check whether the startup script is registered for the page object.
(6) isclientscriptblockregistered Method
Check whether the page object has registered a client script.
In addition to the above methods, the clientscriptmanager class also includes other related methods. Interested readers can read the relevant materials. By using the above methods flexibly in the server control, we can add the control to the client, but also bring the following benefits:
(1) Effectively reduce the size of the displayed page, because many client functions are encapsulated in the client script file, and you only need to reference the address of the script file in the control.
(2) because the same control shares the script file, the browser cache mechanism can improve the application performance.
(3) improved the flexibility and scalability of controls. By modifying script files, control developers can easily modify client functions without compiling server controls.
In addition, in the process of processing complex client functions, you sometimes need to access the control in the client script. The control developer can access and operate the objects rendered to the client in the script. Next, we will briefly introduce some related content.
The control base class has a clientid attribute, which is displayed as the ID attribute of the rendered element. ASP. NET dynamically generates a clientid for a control and ensures that the clientid of each control on the page is unique. Therefore, you can use the control ID in the Document Object Model to access the control on the client (that is, the elements presented by the control ). The control can also use clientid to generate a unique name for any additional elements that it can render (such as hidden fields.
Sending the clientid value to an inline script (or the Code sent to the script Library) may be tricky because the clientid must be inserted in the correct position in the string variable. The following example uses an escape character to insert clientid into a string that constitutes an inline script.
String element = "document. getelementbyid (\" "+ clientid + "\")"; Page. registerarraydeclaration ("page_validators", element ); |
In addition, you can use the format method that is reloaded by the string class to write client scripts using clientid.
4. How to deploy client files
By default, a sub-Folder asp_client exists in the wwwroot folder, which stores client script files that support smart navigation, verification controls, and other functions. Obviously, these files are very important. To improve the application standardization, we recommend that you use the following methods to deploy the client file.
· Place folders containing client scripts in the asp_client folder, especially those installed in the global accessory cache (GAC.
· Names related to controls are recommended for folders containing scripts.
· We recommend that you do not directly place the script file in the folder containing the script, but create a folder named control version number and place the script file in it.
For example, a developer creates a server control mycontrol with Version 1.0, associates it with a client script clientscript. JS, and adds the control to GAC. We recommend that you deploy the client script file in the path c: \ inetput \ wwwroot \ asp_client \ mycontrol_client \ 1.0 \ clientscript. js. In addition, the files stored in the client file library are not only script files, but can also be added toStyle SheetFiles, images, and other files.
The above is a recommended method for deploying client files. Does this mean that all client script files must follow the above rules? Of course not. As long as developers focus on improving the maintainability and convenience of the program from the perspective of reality, it is okay no matter where the client script file is placed.
5. Summary
This article describes how to implement the client functions of server controls. These contents are very important for developing highly interactive server controls. It is not difficult to master the content. What is really difficult is how to develop CSS files, JS scripts, and DHTML programs that have completed client functions. The mastery of these technologies cannot be completed overnight. It requires a solid foundation and painstaking research. All these technical and spiritual qualities are essential to a qualified developer.