Ajax programming in Asp.net-Ajax Server Extension

Source: Internet
Author: User

Ajax server extensions are built on ASP. net2.0 and include a set of new server controls and services to simulate client Ajax behavior. This extension does not follow the traditional Ajax model, but the response method provides end users with this illusion.

1. scriptmanager

The scriptmanger control is used first. It is the brain of Ajax pages and the most important control in the framework. This control must exist on the pages where Ajax is applied. Therefore, it is a good choice to configure this control on the dashboard page.
1.1 understand scriptmanager
Scriptmanger declares in the page, you can use the followingCodeObtain the reference of the scriptmanager object

Scriptmanager = Scriptmanager. getcurrent ( This . Page );

Scriptmanager is mainly responsible for publishing scripts to the browser. The scripts deployed by scriptmanager can be from the ASP. NET Ajax library, local files on the server, or otherProgramCentralized embedded resources.
If this control is added to the page, the script required for Ajax is added to the page. The automatically added script is as follows:

< Script SRC = "/Dawnenterprise/scriptresource. axd? D = ZYr5gMq5Wx2FWpVo0uv-BD-m-oowwmBICEBuKQwKOJhLEbbhej7v8HlVC3cT9q6GK5AIduL5twl1x9QabC7kBb8dwS_WS5k-Bt-8VVrMkv9mVZJQ2-d-i5UAnMs1K7lz0 & amp; T = ffffffffc3daa407" Type = "Text/JavaScript" > </ Script >

in ASP. NET 2.0, embedded resources in an assembly must be accessed through webresource. axd and HTTP processing programs. For webresource, see the following web page:
http://www.codeproject.com/KB/aspnet/MyWebResourceProj.aspx
http://blog.joycode.com/ghj/archive/2008/01/14/113719.joy
while in ASP. net Ajax, it is named scriptresource. replaced by axd's new HTTP handler, which provides some additional features to implement localized browser compression.
the URL contains two parameters: D and T. The D parameter represents the encoded resource key, and the T parameter represents the timestamp, the last modification time of the Assembly. When the page is loaded again, the browser recognizes this parameter. Therefore, you do not need to download the resource again, but use the content cached by the browser.
1.2 deploy a Javascript file
the scriptmanager control has a property scripts, which contains a collection of scriptreference objects. You can include additional script files on the page.

Ajax. scriptmanager
< ASP: scriptmanager ID = "Scriptmanager1" Runat = "Server" >
< Scripts >
< ASP: scriptreference path = " ~ /Scripts/script1.js "   / >
< ASP: scriptreference path = " ~ /Scripts/script2.js "   / >
< ASP: scriptreference assembly = " Demo " Name = " Demo. superscript. js "   / >
</ Scripts >
</ ASP: scriptmanager >  

1.3 Registration Service
JavaScript files are an important aspect of Ajax programming. However, the key to providing Ajax support is that javascript accesses web services to obtain data. To use the Ajax framework to obtain web services, you must register a service reference for the Local Web service.
Scriptmanager has a service attribute that contains a set of servicereference objects. A servicereference object is a service registration mechanism that allows you to access these objects from JavaScript. In.
The following code describes how to use scriptmanager to register a local service.

Ajax. scriptmanager_servicereference
< ASP: scriptmanager ID = "Scriptmanager1" Runat = "Server" >
< Services >
< ASP: servicereference Path = "~ /Services/mainservice. asmx"   />
< ASP: servicereference Path = "~ /Services/testservice. asmx"   />
</ Services >
</ ASP: scriptmanager >

 

2. scriptmanagerproxy

One page has only one scriptmanager control. If multiple scriptmanager controls exist, an invalidoperationexception occurs on the page. However, in some cases, a service or Script Reference is required for a content page, which may not be created by the scriptmanager on the parent page or master page. These references may only be required for a page, other pages are not required. In this case, the scriptmanagerproxy control comes in handy.
Use scriptmanagerprocy on a page to ensure that the script is only included on this page.

Ajax. scriptmanagerproxy
< ASP: scriptmanagerproxy ID = "Scriptmanagerproxy1" Runat = "Server" >
< Scripts >
< ASP: scriptreference path = " ~ /Scripts/dummyscript. js "   / >
</ Scripts >
< Services >
< ASP: servicereference Path = "~ /Services/mainservice. asmx"   />
</ Services >
</ ASP: scriptmanagerproxy >  

Like the parent control, scriptmanagerproxy also has a set of scripts and service references. You can consider scriptmanagerproxy as an extension of the scriptmanager control. It is used to add references not included in scriptmanager. This is common when you use a master page.

 

3. updatepanel Control

The updatepanel control is an Ajax server control that works closely with scriptmanager to update some pages on the page.
The contenttemplate attribute of the updatepanel class defines the dynamically updated area on the page. If updatepanel is used, a normal whole page is not sent back and the whole page is refreshed. A new type of asynchronous PostBack is generated ). asynchronous delivery also goes through the lifecycle of the page. Its processing is similar to regular playback, but does not cause page refresh. This means that the UI and application logic can remain unchanged.
It can check whether the code is being asynchronously sent back:

Ajax. scriptmanger. isinasyncpostback
Protected   Void Page_load ( Object Sender, eventargs E)
{
Scriptmanager = Scriptmanager. getcurrent ( This . Page ); // Return to the scriptmanager instance
If (Scriptmanager. isinasyncpostback ){ // Check asynchronous delivery
// We are doing something cool!
}
} 4. updateprogress Control

During asynchronous delivery, if the processing time is relatively long, you can use this control to tell the user that a loading screen is displayed during data transmission. It has an associatedupdatepanelid attribute that can be set to the relevant updatepannel. In this way, the content in the updateprogress control is displayed only when the related updatepanel is asynchronously sent back.

5. Timer control

This control creates a timer on the client, which calls the send-back function at a specified interval (in milliseconds. Generally, the timer control is placed outside the updatepannel control, and the updatemode attribute in the updatepannel control is conditional, and a tiggers attribute is added to the updatepanel control. The controlid is the ID of the timer and the eventname is tick.

6. handle errors

Generally, an error occurs during the regular delivery process, and the stack track and error information are displayed on the screen. Here, the browser displays exceptions in a dialog box during asynchronous delivery.
For common users, it is not a good practice to Display error messages when exceptions occur. Therefore, the scriptmanager control has an asyncpostbackerror event, which provides a method to display the text in the change dialog box to users. As follows:

Ajax. Exception. asyncpostbackerror
Protected   Void Genres_selectedindexchanged ( Object Sender, eventargs E)
{
Updategenre ();
Throw   New Exception ( " Look out! " ); // An exception is thrown manually during asynchronous delivery.
}
Protected   Void Page_load ( Object Sender, eventargs E)
{
Scriptmanager = Scriptmanager. getcurrent ( This . Page );
// After the specified asyncpostbackerror event occurs, onasyncpostbackerror is called for processing.
Scriptmanager. asyncpostbackerror + =   New Eventhandler < Asyncpostbackerroreventargs > (Onasyncpostbackerror );
}
Void Onasyncpostbackerror ( Object Sender, asyncpostbackerroreventargs E)
{
// The text displayed in the "look out!" dialog box! "->" We're sorry, an unexpected error has occurred"
Scriptmanager. getcurrent ( This . Page). asyncpostbackerrormessage = " We're sorry, an unexpected error has occurred. " ;
}

Of course, this method is not perfect, because after all, the pop-up dialog box is not friendly to the user, so you can use the exception to send the error to the specified error page. This error mechanism can be configured in the customerrors section of Web. config:

< System. Web >
< Customerrors Mode = "On | off | remoteonly" Defaultredirect = "Errorpage. aspx" >   </ Customerrors >
</ System. Web >

If the mode attribute is set to on, it is redirected to the errorpage. ASPX page. If it is set to off, the stack track is always displayed. If it is set to remoteonly, access on a remote machine will redirect the user to the error page.
The scriptmanager control also provides an attribute to override this mechanism. The default allowcustomererrorredirect attribute is set to true. this attribute overwrites the value in customerrors. If allowcustomererrorredirect is set to false, the dialog box is displayed when an exception occurs:

Protected   Void Page_load ( Object Sender, eventargs E)
{
Scriptmanager = Scriptmanager. getcurrent ( This . Page );
Scriptmanager. allowcustomerrorsredirect =   False ;
}

Allowcustomerrorsredirect must be set at page_load (or before). If it is set after load, it does not affect the setting in customerros.

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.