ASP.net 2.0 server Control client Features

Source: Internet
Author: User
Tags contains implement insert object model client
Asp.net| Server | client | control

Most server controls that are applied at the presentation layer consist primarily of two parts: server-side and client functionality. Server-side functionality is always the core of server controls, and as technology develops, client functionality becomes increasingly important. Only two parts work together to create a powerful, interface-rich server control. This article discusses issues related to implementing client-side functionality in server controls, including overview of client functionality, implementation of simple client functionality, implementation of complex client functionality, and implementation of client file deployment.

1. Introduction to Client function

In Web programming, client functionality is traditionally the responsibility of Web page developers and is not encapsulated in server components. asp.net out of this category and enables server controls to emit client script, enabling server controls to combine client-side processing with server-end processing. Implementing client functionality is important to improve the interactivity and scalability of server controls. For example, Common TreeView, TabStrip, toolbar controls, and so on, these excellent server controls have strong interactivity and a rich user interface, and the implementation of these features is inseparable from client functionality.

The techniques for implementing client functionality are primarily client-side scripting (JavaScript, VBScript, etc.) and DHTML. Therefore, as a qualified developer, you must have the ability to skillfully apply these technologies. In addition, there is a way to combine client functionality with server controls. These include methods for implementing simple client functionality, implementing complex client functionality, and deploying client script files.

2. Realize simple client function

If the client-side functionality of a custom server control is simpler, for example, just a pop-up window or a change in the background color, and so on, it is implemented directly in the rendering of the control by not using a separate client script file for encapsulation. The key to its implementation is to add the appropriate client-side handlers in the control's attributes. The following example shows the control Myclickbutton derived from the System.Web.UI.WebControls.Button class, which provides an event handler for the client click event. Take a look at the following code:

public class myclickbutton:button{
Related code
......
protected override void AddAttributesToRender (HtmlTextWriter writer) {
Base. AddAttributesToRender (writer);
Writer. AddAttribute ("onclick", "window.confirm (' Thank you! ');");
}
}

If the reader has already read the previous article about the rendering of the control, it is easy to understand the above code. The above code overrides the AddAttributesToRender method, which defines an attribute named onclick for the Myclickbutton control, and its value indicates that a confirmation window that contains custom information is ejected from the client. If the client-side capabilities of the server controls developed by the reader are simple, you can try to implement them using the rewrite AddAttributesToRender method.

The following is an ASPX page source code created for use with the Myclickcontrol control.

<%@ Page Language = "C #"%>
<%@ ReGISter tagprefix= "Custom" namespace= "mycontrols" Assembly = "MyControls"%>
<body>
<form runat=server>
Please click on the button below
<custom:myclickbutton Id = "Demo" runat=server/>
<br>
</form>
</body>

When the user clicks on the Myclickcontrol control, it immediately pops up a confirmation window containing the message. Note that the pop-up confirmation window is not due to page return, but rather the result of the user firing the client handler. When the "OK" button is clicked, the page return will not occur.

    3. Implementing complex client Capabilities
  
  If the client functionality is complex, Typically, the code that completes its functionality is encapsulated in the client script file. To combine these script files tightly 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 get ClientScriptManager class instances by calling the ClientScript property of the page class. This class is used to manage scripts, register scripts, and add scripts to pages.
  Perhaps readers are unfamiliar with the ClientScriptManager class, which is the new class for ASP.net 2.0. This class is created specifically to replace some of the methods that have been discontinued for the page class that is used to manage scripts, such as RegisterClientScriptBlock, RegisterStartupScript, and so on, that have occurred in ASP.net 1.x, These methods have been discontinued and implemented using the methods associated with the ClientScriptManager class. The
  
  below lists several common ways to implement complex client functionality from the ClientScriptManager class.
 
  (1) RegisterClientScriptBlock method
 
  Adds a script block to the top of the page. Creates a script as a string and passes it to the method, which 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 complete, so you may not be able to refer to all the elements on the page from the script.
  
  (2) RegisterClientScriptInclude method
  
  and RegisterClientScriptBlock method is similar, but this method adds a script block that references an external. js file. Contains files that are added before any other dynamically added script, so you may not be able to reference certain elements on the page.

(3) RegisterStartupScript method

Adds a script block to the page that is executed before the page's onload event is raised after it finishes loading. The script is usually not created as an event handler or function; It usually contains only statements to be executed once.

(4) Registeronsubmitstatement method

Adds a script that is executed in response to the onsubmit event of the page. The script executes before the page is submitted, allowing you to cancel the submission.

(5) IsStartupScriptRegistered method

Determines whether the Page object has a startup script registered.

(6) IsClientScriptBlockRegistered method

Determines whether the Page object registers client script.

In addition to the above several methods, the ClientScriptManager class also includes some other relevant methods, interested readers can read the relevant information. By using the above methods flexibly in server controls, we can complete the addition of client behavior to the control with the following benefits:

(1) Effectively reduce the size of the displayed page, because many of the client-side functionality of the code is encapsulated in the client script file, in the control only need to refer to the script file address.

(2) Because the same control shares script files, it can improve the performance of the application through the caching mechanism of the browser.

(3) Improve the flexibility and scalability of the control. By modifying the script file, the control developer can easily modify the client functionality without compiling the server control.

In addition, in the process of processing complex client functionality, you sometimes need to access the control in client script. The control developer can access the object rendered to the client in the script and manipulate it. Here's a quick introduction to some of the things.

The control base class has a ClientID property that is rendered as the id attribute of the rendered element. Asp. NET dynamically generates ClientID for a control and ensures that the clientid of each control on the page is unique. Therefore, you can access the control (that is, the element that is rendered by the control) on the client by using the ID of the control in the Document Object model. Control can also use ClientID to generate a unique name for any additional elements that it can render, such as hidden fields.

Sending a ClientID value into an inline script (or in code emitted to the script library) can be tricky because the ClientID must be inserted in the correct position in the string variable. The following instance uses an escape character to insert ClientID into the string that forms the inline script.

string element = "document.getElementById (\" "+ ClientID +" \ ")";
Page.registerarraydeclaration ("Page_Validators", Element);

In addition, you can use the overloaded format method of the string class to compose client script that uses ClientID.

4. Ways to deploy Client files

Under the default installation, a subfolder asp_client exists under the Wwwroot folder, which holds client script files that support features such as smart navigation, validation controls, and so on. It is clear that these documents are very important. To improve application standardization, it is recommended that developers use the following methods during the deployment of client files.

· Place folders that contain client script in the Asp_client folder, especially for controls that are installed in the global Accessories cache (GAC).

· The folder that contains the script recommends using the name associated with the control.

· It is recommended that you do not place the script file directly in the folder containing the script, but instead create a folder called the version number of the control to place the script file.

For example, a developer creates a server control mycontrol with version 1.0 that associates a client script clientscript.js and adds the control to the GAC. This is the recommended path for deploying client script files: c:\Inetput\wwwroot\asp_client\MyControl_Client\1.0\ClientScript.js. In addition, you can include not only script files in the client file library, but also other files such as style sheet files, pictures, and so on that the control requires.

The above is the recommended way to deploy client files, so does this mean that all client script files need to follow these rules? Of course not. As long as the developer is actually starting from the point of view of improving the maintainability and convenience of the program, it is possible regardless of where the client script files are placed.

5. Summary

This article mainly introduces the content related to implementing server Control client function. These are important for developing a highly interactive server control. It is not difficult to master these content, the real difficulty is how to develop the client function of the CSS file, JS script, DHTML programs and so on. The mastery of these technologies can not be completed overnight, it needs a solid foundation, assiduous study. And all of these technical and spiritual quality is a qualified developer must grasp the essence of the indispensable.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.