ASP. NET Ajax (2): Test scriptmanager

Source: Internet
Author: User

1 Overview scriptmanager control management for Microsoft ASP. NET
The client script of the Ajax page. By default, the scriptmanager Control
The library script is registered with the page so that the script can be extended using the type system and support partial page output and web service calls.
On the page, you must use the scriptmanager control to make the following Microsoft ASP. NET Ajax features available:
    • The client Script Function of the Microsoft Ajax library and any custom scripts to be sent to the browser.
    • Some page outputs, so that a certain area of the page can be refreshed without relying on delivery. The updatepanel, updateprogress, and timer controls of ASP. NET Ajax require the scriptmanager control to support partial output.
    • Web Service JavaScript proxy, which allows client scripts to access the methods exposed by strong Web Services.
    • Javascript access ASP. NET authentication and personalized application services.

1.1 make some page outputs available when one or more updatepanel controls exist in the page, the scriptmanager control manages local page outputs in the browser. Controls interact with page generation cycles to update partial pages contained in the updatepanel control.
Scriptmanager
Enablepartialrendering
The attribute determines whether the page can be updated locally. By default, the value of enablepartialrendering is true. Therefore,
When the scriptmanager control arrives at the page, local output is available.

1.2 during local output, you can use the following methods to handle errors:

    • Set the allowcustomerrorsredirect attribute, which determines how to use the custom error section of the web. config file when an error occurs during asynchronous sending.
    • Handles the asyncpostbackerror event of the scriptmanager control, which is triggered when a page error occurs during asynchronous sending.
    • Set the asyncpostbackerrormessage attribute, which is an error message sent to the browser.

1.3 Use the Type System Extension Microsoft
Javascript is added to the Ajax library.
Provides namespace, inheritance, interfaces, enumeration, reflection, and auxiliary functions for strings and arrays. These extensions provide functions in client scripts to make them look like
. NET Framework. It allows you to write ASP. NET 2.0 Ajax in a structured way.
Extended applicationsProgramTo enhance maintainability, make it easier to add features and divide functional layers. Apply the scriptmanager control to ASP. NET web
The page automatically contains the type system extension, so you can use the library in the client script.

1.4 register a custom script to enable
Use scriptmanager to manage resources created for controls that participate in partial page updates. resources include scripts, styles, hidden fields, and arrays. Scriptmanager
The scripts set of the control includes the scriptreference object of the scripts available for each browser. You can specify a script using declaration or programming.
Scriptmanager
Controls also provide methods for managing client scripts and hiding fields programmatically. You must call
Scriptmanager registration method. (You can use the clientscriptmanager class to register scripts not required for partial page updates .)
Note: any script registered by the scriptmanager control on the page and all event processing scripts must be included in the <form> element on the page. Otherwise, the script will not be registered or executed.

1.5 Register web service scriptmanager
The services set of the control contains the servicereference of each Web Service registered using scriptmanager.
Object. The ASP. NET Ajax framework is for each servicereference in the services set.
Object To generate a client proxy object. The proxy class and its strongly-typed members simplify the use of web services by client scripts.
You can add the servicereference object to the services collection programmatically during running to register the web service.

1.6 using authentication and Personalization Services in client scripts the Microsoft Ajax library contains a proxy class that calls ASP. NET 2.0 form authentication and personalized application services directly from JavaScript. To use a custom authentication service, you can use the scriptmanager control to register the service.

1.7 The scriptmanagerproxy class is in
Only one scriptmanager can be added to a page.
Control. A page can contain controls directly or nested components, such as user controls, content pages on a master page, or nested master pages. When there is already
The scriptmanager control. When nested or parent components require other features of the scriptmanager control, the component can contain
The scriptmanagerproxy control. For example, the scriptmanagerproxy control allows you to add scripts and services for nested components only to corresponding components.

2. Add the scriptmanager control and drag the scriptmanager control from the toolbox to the <form> element of the page to get the following element reference. In this way, ASP can be used on all its pages. net Ajax. <Asp: scriptmanager id = "scriptmanager1" runat = "server"> </ASP: scriptmanager>

3 client script management examples 3.1 and 3.2 respectively demonstrate how to add the scripts or independent script files embedded in a single program to the page. If you want to add multiple scripts at the same time, add multiple <asp: scriptreference> elements to the <scripts> element by following these two methods.

3.1 Add the script embedded in the Assembly as follows:CodeShows the previesscript. js script file embedded in the microsot. Web. Preview assembly to the page. <Asp: scriptmanager id = "scriptmanager1" runat = "server">
<Scripts>
<Asp: scriptreference Assembly = "microsot. Web. Preview" name = "previesscript. js"/>
</Scripts>
</ASP: scriptmanager>

3.2 Add a separate script file the following code shows how to add a separate script file to the page ~ /Scriptpath/scriptfile. js. you can replace it with the script file you need. <Asp: scriptmanager id = "scriptmanager1" runat = "server">
<Scripts>
<Asp: scriptreference Path = "~ /Scriptpath/scriptfile. js "/>
</Scripts>
</ASP: scriptmanager>

3.3 scriptmanager attributes scriptmode: scriptmanager
And the scriptreference object both have the scriptmode attribute. It 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. Debug and release
The meaning is very clear and will not be explained. Inherit indicates inheriting the outer settings. For scriptmanager, it is equivalent to auto. Auto
Indicates that the script version is automatically determined based on the current compilation mode and related settings in Web. config.

Scriptpath: Used
The relative, absolute, or application-related URLs of all script blocks on the specified page, including custom or third-party script blocks registered by the current scriptmanager instance. If
In the scripts set, the scriptreference sets the path attribute, which overwrites the scriptpath
. If this attribute is not set and scriptreference specifies an assembly, webresource is used for each request. after the axd parses the script from the assembly and returns it to the browser, the JS file under scriptpath will be used directly after the setting.

Loadscriptsbeforeui: When this attribute is true, the <SCRIPT> element generated for the introduced script appears before the UI tag of all pages; otherwise, it is generated after all UI tags. If you need to use the imported script for execution during the loading process, you must set it to true. Otherwise, the script cannot be executed.

4. Adding a web service client proxy is similar to adding a web service client proxy, as shown in the following code: <asp: scriptmanager id = "scriptmanager1" runat = "server">
<Services>
<Asp: servicereference Path = "myservices. asmx"/>
</Services>
</ASP: scriptmanager>

In addition, you must add the following configuration items to Web. config: <system. Web>
<Httphandlers>
<Remove verb = "*" Path = "*. asmx"/>
<Add verb = "*" 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>

In this way, you can directly call the static method of the web service in the client script.

5. Here is only the simplest way to handle exceptions in asynchronous sending.
generally, when an exception occurs ,. net Framework will redirect to a yellow exception page, but for pages processed in asynchronous sending-back mode, this will be a disaster and cannot be restored to the current status of the original page.
you can set the allowmermerrorsredirect = "false"
of the scriptmanager control to prevent redirection in case of an exception, set asyncpostbackerrormessage
to the error message displayed to the user when an exception occurs. In this way, when an exception occurs, scriptmanager will automatically pop up a browser message box containing the information set in
asyncpostbackerrormessage.

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.