[ASP] The ScriptManager control uses

Source: Internet
Author: User

Directory

Overview

Local Refresh

Error handling

Type System extensions

Registering Custom Scripts

Registering a Web service

Using authentication and Personalization services in client script

ScriptManagerProxy class

Adding ScriptManager controls

Client Script Management sample

Overview

The ScriptManager control manages client script for Microsoft ASP. NET AJAX pages. By default, the ScriptManager control registers the script of the Microsoft AJAX library with the page, which allows the script to use the type system extensions and support local page output and Web service invocation.

In the page, you must use the ScriptManager control to make the following Microsoft ASP. NET AJAX Features available:

The client-side scripting features of the Microsoft AJAX Library, as well as any custom scripts to send to the browser.

Partial page output, which allows a region in the page to be refreshed without having to rely on loopback. The UpdatePanel, UpdateProgress, and Timer controls for ASP. NET AJAX require ScriptManager controls to support local output.

The Javascript proxy for Web services, which enables client script to access methods exposed by strongly typed Web services.

Javascript classes Access ASP. NET authentication and personalization application services.

Local Refresh

When there is one or more UpdatePanel controls in the page, the ScriptManager control manages the local page output in the browser. Control interacts with the page generation cycle to update the local page contained in the UpdatePanel control.

The EnablePartialRendering property of the ScriptManager control determines whether the page can be partially updated. By default, the value of enablepartialrendering is true, so when a ScriptManager control is added to the page by default, the local output is available.

Error handling

During partial output, you can use the following methods to handle errors:

Sets the Allowcustomerrorsredirect property, which determines how to use the custom error section of the Web. config file when an error occurs during an asynchronous postback.

Handles the Asyncpostbackerror event of the ScriptManager control, which is triggered when a page fault occurs during an asynchronous postback.

Sets the Asyncpostbackerrormessage property, which is the error message sent to the browser.

Type System extensions

The Microsoft AJAX Library adds a Javascript-type system extension that provides namespaces, inheritance, interfaces, enumerations, reflections, and accessibility for strings and arrays. These extensions provide functionality in client script to make it look like the. NET framework. It enables you to write ASP. NET 2.0 AJAX extension applications in a structured way to enhance maintainability, make it easier to add features, and partition functional hierarchies. The ScriptManager control to an ASP. NET Web page automatically contains the type system extension, so you can use the library in client script.

Registering Custom Scripts

Use ScriptManager to manage resources that are created for controls that participate in local page updates, including scripts, styles, hidden fields, and arrays. The Scripts collection of ScriptManager controls includes ScriptReference objects for each script available to the browser. You can specify scripts by declaring or programming them.

The ScriptManager control also provides registration methods that you can use to programmatically manage client script and hidden fields. When registering scripts and hidden fields that support local page updates, you must call the ScriptManager registration method. (You can use the ClientScriptManager class to register scripts that are not required for local page updates.) )

Attention:
Any script and all event-handling scripts that are registered in the page by the ScriptManager control must be included in the <form> element in the page, otherwise the script will not be registered or executed.

Registering a Web service

The ScriptManager control's Services collection contains a ServiceReference object for each WEB service that is registered with ScriptManager. The ASP. NET AJAX framework generates a client proxy object for each ServiceReference object in the Services collection. The proxy class and its strongly typed members simplify the use of WEB services by client script.

You can programmatically add the ServiceReference object to the Services collection at run time to register the WEB service.

Using authentication and Personalization services in client script

The Microsoft AJAX Library contains proxy classes that invoke ASP. NET 2.0 Forms authentication and Personalization app services directly from Javascript. If you want to use a custom authentication service, you can use the ScriptManager control to register.

ScriptManagerProxy class

You can add only one ScriptManager control to a page. A page can contain a control directly or in a nested component, such as a user control, a content page in a master page, or a nested master page. A component can contain a ScriptManagerProxy control when there is already a ScriptManager control in the page, and the nested or parent component needs to ScriptManager the other attributes of the control. For example, the ScriptManagerProxy control lets you add scripts and services that are used only for nested components into the appropriate component.

Adding ScriptManager controls

Simply drag the ScriptManager control from the Toolbox onto the <form> element of the page to get the following element reference, so that you can use ASP. NET AJAx on all of its pages.

1 <  ID= "ScriptManager1"  runat= "Server"></asp: ScriptManager>

Client Script Management sample

(1) and (2) shows the method of adding a script embedded in a single assembly or a standalone script file to the page, and if you want to add more than one script at a time, add more than one <asp:scriptreference to the <Scripts> element by any of these two methods > elements can be.

(1) Adding scripts embedded in an assembly

1 <Asp:scriptmanagerID= "ScriptManager1"runat= "Server"> <Scripts> <asp:scriptreference Assembly="Microsot.Web.Preview"Name="Previesscript.js" /> </Scripts> </asp:scriptmanager>

(2) Add a separate script file

The following code shows the addition of a separate script file to the page

~/scriptpath/scriptfile.js, you can replace it with the script file you need.

1 <Asp:scriptmanagerID= "ScriptManager1"runat= "Server"> <Scripts> <asp:scriptreference Path="~/scriptpath/scriptfile.js" /> </Scripts> </asp:scriptmanager>

(3) Related properties of ScriptManager

ScriptMode:

Both the ScriptManager and ScriptReference objects have the ScriptMode property, which determines whether the version of the script sent to the client is Debug or release. It has four optional values: Inherit, Auto, Debug, and Release. The meaning of Debug and Release is very clear and is no longer explained. Inherit represents the inheritance of the outer layer setting, which is equivalent to Auto for ScriptManager. Auto indicates that the script version is automatically determined based on the current compilation mode and related settings in Web. config.

ScriptPath:

A relative, absolute, or application-related URL that specifies all the script blocks in the page, including custom or third-party script blocks registered by the current ScriptManager instance. If ScriptReference in the Scripts collection has the Path property set, the Path property overrides the ScriptPath setting. If this property is not set and ScriptReference specifies an assembly, each time the script is requested, the script will be parsed out of the assembly with WebResource.axd and then sent back to the browser, and the JS file under ScriptPath will be used directly after Setup.

Loadscriptsbeforeui:

When this property is Ture, the generated <script> elements for the introduced script will appear before the UI markup for all pages, otherwise generated after all UI tags. If the loaded procedure executes a script that requires the introduction of the script, it must be set to true, otherwise it will not execute.

4 Adding a WEB service client Agent

The client proxy for adding a WEB service is similar to adding a script, as shown in the following code:

1 <Asp:scriptmanagerID= "ScriptManager1"runat= "Server"> <Services> <asp:servicereferencePath= "Myservices.asmx" /> </Services> </Asp:scriptmanager>

In addition, the following configuration items need to be added to the Web. config:

1 <system. Web> <httphandlers> <Removeverb="*"Path= "*.asmx"/> <Addverb="*"Path= "*.asmx"Validate= "false"type= "System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, version=1.0.61025.0, culture= Neutral, publickeytoken=31bf3856ad364e35 "/> </httphandlers> </system.web> 

This allows the static method of the Web service to be invoked directly in the client script.

5 handling exceptions that occur in an asynchronous postback

Only the simplest way is described here.

Typically, when an exception occurs, the. NET Framework is redirected to a yellow exception page, but for pages processed in an asynchronous postback, this can be a disaster and cannot revert to the current state of the original page.

You can set the Allowcustomerrorsredirect = "false" of the ScriptManager control to prevent redirection when an exception occurs, set Asyncpostbackerrormessage property is the error message that is displayed to the user when an exception occurs. This way, when an exception occurs, ScriptManager automatically pops up a browser message box that contains the information set in Asyncpostbackerrormessage.

Reference

Baidu Library: "Ajax in the ScriptManager combat "

[ASP] The ScriptManager control uses

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.