The most important controls in the infrastructure of ASP. NET Ajax server include scriptmanager and scriptmanagerproxy ). Each ASP. NET Ajax page contains only one instance of the scriptmanager control. If the ASP. NET page does not contain the scriptmanager control, you cannot enable any Ajax function. The scriptmanagerproxy control can only be used in the master page environment to reference the source script manager on the Content Page.
The scriptmanager control is used to manage and distribute script resources so that client scripts can use the Microsoft Ajax framework. Use the following code to add the script manager to the page:
<asp:ScriptManager runat="server" ID="ScriptManager1" />
This control does not generate a user interface and is only valid on the server side. It does not add any additional data to the client page.
Attributes of the scriptmanager Control
The attributes of the scriptmanager control are shown in the following table:
The script manager is the hub of ASP. NET Ajax pages.
Scriptmanager Control Method
The following table lists the main methods of the scriptmanager control:
All static methods here are used to add some form of script or tag to the client page. These static methods define the Ajax version of the corresponding method in the clientscript object on the page.
What is the difference between the registerxxx method of the scriptmanager control and the corresponding method of the clientscript object (instance of the clientscriptmanager object) on the page?
The registration methods of clientscriptmanager and scriptmanager are the same, but they are used differently. We only use the scriptmanager method to add the required script code when executing the callback operation in the Ajax section. In addition to the different rendering stages, the runtime will process the callback operations of Ajax in the conventional way. At this stage, the script code is generated and all registered scripts are added. Because scriptmanager is responsible for generating tags during Ajax sending back, scriptmanager can be added only after it obtains the registered script. If you use the clientscriptmanager method on the Ajax page, no script may be added during the update panel refresh process.
Events of the scriptmanager Control
The following table lists two events supported by the scriptmanager control:
The following code is an example of the resolvescriptreference event. We can change the path of the script to be downloaded to the client:
protected void ResolveScript(object sender, ScriptReferenceEventArgs e)
{
if(string.Equals(e.Script.Path, "personal.js", StringComparison.OrdinalIgnoreCase))
{
e.Script.Path = "person.js";
}
}
The following is an example of asyncpostbackerror event. It can display different error messages based on different IP addresses:
protected void AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
{
ScriptManager sm = sender as ScriptManager;
if(Request.UserHostAddress == "127.0.0.1")
{
sm.AsyncPostBackErrorMessage = string.Format("<b>An error occured.<br/>{0}<b>", e.Exception.Message);
}
else
{
sm.AsyncPostBackErrorMessage = string.Format("<b>An error occured.<br/>{0}<b>", "Please contace your web master.");
}
}
If an exception is thrown during execution of some rendering operations, the HTTP status code returned by the HTTP request is 200, and a complete description of the error is provided without marking the update.
Scriptmanagerproxy Control
You can add the <asp: scriptmanager> label to the page or import components that already contain the script manager. Generally, you can import user controls, create content pages for master pages, or write nested master pages to the existing script manager.
If the content page needs to add a new script reference to the Manager, what should I do? In this case, we need a reference from the script manager. Therefore, we can use the getcurrent method of scriptmanager to obtain the current script manager instance:
sm = ScriptManager.GetCurrent(this.Page);
With the scriptmanagerproxy class, we do not need to complete this encoding. You can add the scriptmanagerproxy control on the content page when you need the scriptmanager control function and lack its reference.
There cannot be two script managers in the context of the same page, but we can reference the same script manager and obtain it through the agent. When a proxy is used, the scripts and services collections of the scriptmanager and scriptmanagerproxy controls are merged at runtime.
The scriptmanagerproxy class encapsulates the getcurrent method of the scriptmanager class. It is very simple and has limited access attributes.
Script generation and Loading
The scriptmanager control allows us to avoid a lot of maintenance-free <SCRIPT> labels and provide rich functions.
We can use the scripts collection to let scriptmanager know the script file we want to add to the page. In addition to the script requested by the user, the scriptmanager control automatically adds the script required for ASP. NET ajax to the client page. In this way, we don't have to worry about linking to the Microsoft Ajax library or other inherent functions of ASP. NET Ajax. The following code demonstrates the Script Loading Model:
<asp:ScriptManager runat="server" ID="ScriptManager1">
<Scripts>
<asp:ScriptReference Name="YourCompany.ScriptLibrary.CoolUI.js"
Assembly="YourCompany.ScriptLib" />
<asp:ScriptReference Path="~/Scripts/MyLib.js" />
</Scripts>
</asp:ScriptManager>
The following table lists the attributes of scriptreference for control Script Loading:
If you look at the HTML source code of the ASP. NET Ajax page, we can hardly find a reference to the important script file microsoftajax. JS, but can only see the following code:
<script src="/Core35/ScriptResource.axd?d=...&t=..."
type="text/javascript">
</script>
The reference of the script comes from embedded web resources, which are returned by the HTTP handler scriptresource. axd. In ASP. NET Ajax, this processing program replaces the webresource. axd processing program built in ASP. NET 2.0. What is the difference between scriptresource. axd? In addition to providing Script Reference, the scriptresource. axd handler also adds various localized JavaScript resources to the requested file.
Processing of debugging and publishing script files
Compared with the traditional <SCRIPT> label, scriptmanager provides additional services to link script files of the debug or publish version as needed. ASP. NET uses a special naming convention to differentiate debugging and publishing versions of script files. If the released version of the script file is called script. JS, its debugging version is script. Debug. js.
The scriptmanager control can perform specific work and distinguish the two sets of scripts based on the above naming conventions. If the debug of the <compilation> section in the web. config file is true, the scriptmanager control selects the script for debugging.
Localized scripts
Script files can have localized elements. If the enablescriptlocalization attribute is true and the UI culture is set properly on the page, the script manager automatically obtains the corresponding script file based on the current regional settings.
Local execution depends on the uicultrue attribute of the @ page command and the uiculture attribute of the page class:
<%@ Page Language="C#" UICulture="it-IT" ... %>
We can specify the UI culture for each referenced script:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnableScriptLocalization="true">
<Scripts>
<asp:ScriptReference Path="Person.js" ResourceUICultures="it-IT" />
</Scripts>
</asp:ScriptManager>
Note: If you do not specify the path attribute of the tag referenced by the script, resourceuicultures will be ignored.
In the above example, the scriptmanager object tries to get the script file named person. it-IT.js in the path specified path.
Globalization of scripts
If the enablescriptglobalization attribute is true, scriptmanager adds a script for setting the global object SYS. cultureinfo on the client. The javascript class uses it and displays the content based on the regional settings.