Build Custom Controls 5-integrate custom controls with JavaScript

Source: Internet
Author: User
Tags javascript array

Learn the methods and attributes of the clientscriptmanager class, learn how to use the httpbrowsercapabilities class to detect different browser versions, and learn Ajax. The clientscriptmanager class exposes the main application interfaces using client scripts. The httpbrowsercapabilities class allows us to develop controls for different types of browsers, ajax allows us to implement custom controls without having to submit pages containing controls to the Web server for communication.

5.1 about the clientscriptmanager class

Clientscriptmanager supports adding JavaScript to the page:

1) registerarraydeclaration -- add a JavaScript array to the page;

2) registerclientscriptblock -- add the Javascript script to the start server of the page after the <form> tag;

3) registerclientscriptinclude -- add the start server <form> label after the external JS file is referenced to the page;

4) registerclientscriptresource -- add JS compiled into the assembly to the page;

5) registerexpandattribute -- add a script to add an extension attribute to an element on the page;

6) registerhiddenfield -- add a hidden form field after the <form> tag of the start server of the page;

7) registeronsubmitstatement -- add the JS script executed before the page is sent back to the server;

8) registerstartupscript -- add a JS script before the end server <form> label on the page.

These methods can be safely called multiple times without repeated addition.

Some other attributes can be used to check whether a script has been registered on the page:

1) isclientscriptblockregistered -- returns true if a script is registered to the page using the registerclientscriptblock () method;

2) isclientscriptincluderegistered -- returns true if an external script file reference has been registered to the page through the registerclientscriptinclude () method;

3) isonsubmitstatementregistered -- returns true if a script has been registered to the page using the registeronsubmitstatement () method;

4) isstartupscritpregistered -- returns true if the party script has been registered to the page through the registerstartupscript () method;

5.2 check the browser function

The httpbrowsercapabilities object is public through the request object. You can use request. browser to obtain the reference of the object. E.g.

If (request. browser. browser. Equals ("ie") & request. browser. majorversion> = 5)

{

Page. response. Write (request. browser. browser );

}

The httpbrowercapabilities object uses the User-Agent header sent by the browser to detect the function of the browser.

 

Sample Code:

Newwindowlink. CS, webwindow. CS, clienttab. CS

 

5.3 create an Ajax Control

To implement Ajax in a custom control, perform the following steps:

1. Generate JavaScript that triggers Ajax calls;

2. Create a method to respond to Ajax calls on the server;

3. create javascript in the browser to display the results returned by the server.

Ajax calls are triggered to execute server-side methods, and the results of server-side execution are returned to the client for display.

Use the page. clientscripts. getcallbackeventreference () method to create javascript that triggers Ajax calls. This method returns a string that indicates JavaScript function calls, as shown in:

Webform_docallback ('mycontrol', null, showresult, null, showerror, false)

Some overload parameters of the getcallbackeventreference () method:

Control -- triggers Ajax controls;

Argument -- parameters passed to the server in Ajax calls;

Clientcallback -- Name of the JavaScript function executed when the result is returned from the Web server;

Context -- parameters of the clientcallback () and clienterrorcallback () methods are returned after Ajax is called.

Clienterrorcallback -- Name of the JavaScript function executed when an error result is generated on the service end during the Ajax call

Useasync -- if this parameter is set to true, Ajax calls asynchronous execution.

To achieve this goal, you must implement the icallbackeventhandler interface.

Two Methods of the icallbackeventhandler interface:

Raisecallbackevent -- this method is first executed for processing during callback;

Getcallbackresult -- returns the result to the client.

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.