ScriptManager Control in ASP.NET

Today I would like to talk about the ScriptManager control available in Microsoft AJAX. This small article is intended for the audience new to AJAX.

Talking about AJAX (Asynchronous JavaScript and XML), it is a technique to create better, faster and interactive web pages using HTML, CSS and XML and JavaScript. Along with this it is also used for partial postback (sending part of data to the server). You can say AJAX, as a web browser technology, works on the client side and is independent of the web server.

Visual Studio has provided a tool to implement AJAX in our web application. To use AJAX in ASP.NET webpages, first you need to add the ScriptManager control available in your toolbox of Visual Studio.

ScriptManager control
Figure 1 shows the tag that would be added in Html markup page.

Why do we need the ScriptManager?

This ScriptManager control provides support for client-side AJAX features in an AJAX enabled web pages.

It actually registers and loads the Microsoft AJAX library to enable the AJAX features.

What are the features of the ScriptManager control?

  1. Add a JavaScript proxy class for Web Services. This will help you to call the Web Service from JavaScript.

    features of ScriptManager control

    As shown above, when you register the Web Service reference, the Microsoft AJAX framework generates a proxy of the web service so that it can be accessed from JavaScript directly with the WebMethod name.
    1. function CallService() {  
    2. WebService1.GetInfo("1", onSuccess, onFailure, null);  
    3. }  
    4. function onSuccess(result) {  
    5. alert(result);  
    6. }  
    7. function onFailure(result) {  
    8. alert("Error occured");  

    This makes it easy for the developer to use a WebService call from JavaScript.
     
  2. Registering and loading Custom Client Script.
    Custom Client Script
    The preceding figure shows how to add custom script files that you may need along with AJAX. You can add one or more .js files in a similar fashion.
     
  3. Using Authentication, Profile and Role Services from Client Script

    To enable authentication from a client script, you need to explicitly enable authentication service in the web config file. This will allow Microsoft AJAX to include proxy classes for ASP.NET 2.0 form authentication. Similarly for enabling a profile service and role service we need to add a respective tag as shown below.
    1. <system.web.extensions>  
    2. <scripting>  
    3. <webServices>  
    4. <authenticationService enabled="true" />  
    5. <profileService enabled="true" />  
    6. <rolesService enabled="true" />  
    7. </webServices>  
    8. </scripting>  
    9. </system.web.extensions> 
  4. Partial Page rendering is where a region of a page can be sent to the server and refreshed without a postback. This is a very important feature of AJAX.

    It is possible using the UpdatePanel control. All the controls within this panel take part in a partial postback. But to make this possible the ScriptManager control should be included on top before using UpdatePanel. This call can be made Asychronous.

Note: We can have only a single instance of the ScriptManager control in our webpage.

ScriptManagerProxy

As I have said above, we can have only single instance of the ScriptManager control in our webpage.

The page may contain many usercontrols and a usercontrol may require some additional custom script to be loaded to work properly. You want that register this script only when I use that usercontrol in a page. At that time you can use a ScriptManagerProxy control to register the custom scripts or generate a proxy for the webservice and so on.

The ScriptManagerProxy works in a similar fashion as the ScriptManager control.

ScriptManagerProxy

You can have multiple ScriptManagerProxy item in a page. This helps you to add custom scripts or a Webservice proxy only when needed.

Conclusion

Hence I conclude by saying, to enable AJAX in ASP.NET, a ScriptManager and ScriptManagerProxy is a mandatory control to be included in any Web page. I hope you like this small article. Don't forget to share your comments, whether it's good or bad. Sharing is valuable no matter what.

References