Click here to Skip to main content
15,886,110 members
Articles / Web Development / ASP.NET

ASP.NET AJAX Controls and Extenders

Rate me:
Please Sign up or sign in to vote.
4.80/5 (36 votes)
13 Sep 2012LGPL351 min read 314.3K   4.5K   200  
This tutorial examines the new Visual Studio 2008 Server Control and Server Control Extender. A compendium of tips, tricks, and gotchas, it is a comprehensive tutorial that will provide readers with the skills necessary to start building advanced AJAX-enabled custom controls with Visual Studio.
/// <reference name="MicrosoftAjax.js"/>


Type.registerNamespace("SessionTimeoutTool");

SessionTimeoutTool.TimeoutWatcherBehavior = function(element) {
    SessionTimeoutTool.TimeoutWatcherBehavior.initializeBase(this, [element]);
    this._interval = 1000; 
    this._message = null;
    this._timeoutMode = null;
    this._timer = null;
    this._redirectPage = null;
    this._customHandlerJScript = null;
    this._fromExtender = false;
    this._popupBehavior = null;
}

SessionTimeoutTool.TimeoutWatcherBehavior.prototype = {
    initialize: function() {
        SessionTimeoutTool.TimeoutWatcherBehavior.callBaseMethod(this, 'initialize');
        //create timer
        this._timer = new Sys.Timer();
        //create timer handler           
        tickHandlerDelegate = Function.createDelegate(this, this.tickHandler);
        this._timer.add_tick(tickHandlerDelegate);
        //create onload handler
        setTime = Function.createDelegate(this,this.setTimer);
        Sys.Application.add_load(setTime);

        if(this._timeoutMode == SessionTimeoutTool.Mode.PopupMessage && this._fromExtender == true)
        {
            //create popup behavior
            this._popupBehavior = $create(AjaxControlToolkit.PopupBehavior
            , {"id":this.get_id()+'PopupBehavior'}
            , null
            , null
            , this.get_element());
        }
    },
     setTimer:function()
     {
        if(this._timer)
        {
            this._timer.set_enabled(false);
            if(this._timeoutMode == SessionTimeoutTool.Mode.ExtendTime)
                this._timer.set_interval(this.get_interval()- 45000);
            else
                this._timer.set_interval(this.get_interval());
            this._timer.set_enabled(true);
        }
     },
     tickHandler: function(){
        if(this._timeoutMode == SessionTimeoutTool.Mode.PageRedirect)
        {
            this.pageRedirect();
            return;
        }
        if(this._timeoutMode == SessionTimeoutTool.Mode.PopupMessage)
        {
            this.popup();
            return;
        }
        if(this._timeoutMode == SessionTimeoutTool.Mode.ExtendTime)
        {
            this.extendTime();
            return;
        }
        if(this._timeoutMode == SessionTimeoutTool.Mode.CustomHandler)
        {
            this.customHandler();
            return;
        }  
    },
    pageRedirect: function(){
        window.location = this._redirectPage;
    },
    popup: function(){
        this._timer._stopTimer();
        if(this._fromExtender == true)
        {
            this._popupBehavior.show();
            return;
        }
        if(this._message == null)
        {
            alert("The session has expired.");
        }
        else
        {
            alert(this._message);
        }
    },
    extendTime: function(){
        this.callWebService();
    },
    callWebService: function()
    {
        var webRequest = Sys.Net.WebServiceProxy.invoke(
        "SessionTimeoutTool.asmx" //path
        , "ExtendSessionTimeout" //methodName
        , false //useHttpGet
        , null //parameters 
        , this.succeededCallback
        , this.failedCallback
        , "User Context");//userContext 
    },
    succeededCallback: function(result, eventArgs)
    {
        if(result !== null)
            alert(result);
    },
    failedCallback: function(error)
    {
        alert(error);
    },
    customHandler: function(){
        this._timer._stopTimer();
        eval(this.get_customHandlerJScript());
    },
    get_interval: function() {
        return this._interval;
    },
    set_interval: function(value) {
        this._interval = value;
    },
    get_message: function()
    {
        return this._message;
    },
    set_message: function(value)
    {
        this._message = value;
    },
    get_timeoutMode: function()
    {
        return this._timeoutMode;
    },
    set_timeoutMode: function(value)
    {
        this._timeoutMode = value;
    },
    get_redirectPage: function()
    {
        return this._redirectPage;
    },
    set_redirectPage: function(value)
    {
        this._redirectPage = value;
    },
    get_customHandlerJScript:function()
    {
        return this._customHandlerJScript;
    },
    set_customHandlerJScript:function(value)
    {
        this._customHandlerJScript = value;
    },
    get_fromExtender: function()
    {
        return this._fromExtender;
    },
    set_fromExtender: function(value)
    {
        this._fromExtender = value;
    },
    dispose: function() {        
        SessionTimeoutTool.TimeoutWatcherBehavior.callBaseMethod(this
        , 'dispose');
        if (this._timer) {        
            this._timer.dispose();
            this._timer = null;
        }
        $clearHandlers;
    }
}
SessionTimeoutTool.TimeoutWatcherBehavior.registerClass('SessionTimeoutTool.TimeoutWatcherBehavior', Sys.UI.Control);

SessionTimeoutTool.Mode = function(){};
SessionTimeoutTool.Mode.prototype = 
{
    PageRedirect: 0,
    PopupMessage: 1,
    ExtendTime: 2,
    CustomHandler: 3
}
SessionTimeoutTool.Mode.registerEnum("SessionTimeoutTool.Mode");

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior)
United States United States
James is a program writer for a respectable software company. He is also a Microsoft MVP.

Comments and Discussions