Home / Community & Support / Knowledge Base / RadControls for ASP.NET AJAX / General, Installation, Licensing / Redirect to a log-in page if Session expires during Load On Demand callback

Redirect to a log-in page if Session expires during Load On Demand callback

Article Info

Rating: Not rated

Article information

Article relates to

 RadComboBox, RadTreeView, etc. 

Last modified

 8/11/2010

Last modified by

 8/11/2010


THE ISSUE

The ASP.NET AJAX framework’s callback mechanism is designed to process only three types of responses from the server:
  • Success
  • AJAX Exception
  • ASP.NET Exception
Each of these responses is formatted in a specific way so that the client-side script processing the callback recognizes them and decides which events to rise in each case. If, however, the server returns something different, for instance an HTTP status code indicating a redirection or 401 Unauthorized/403 Forbidden, the ASP.NET AJAX script does not know what to do, hence nothing happens in response to this callback.

Since RadControls are based on the ASP.NET AJAX Framework it is clear that if the same as above happens during a Load on Demand request, the control will ‘hang’. For instance RadcomboBox will remain open saying ‘Loading…’ indefinitely:


THE SOLUTION

In this specific case, the ASP.NET AJAX client-side framework does not allow much to be done through its API in an elegant manner. This is why the only way to work around this limitation is to ‘patch’ the function that decides which events to rise based on the response of the callback, namely WebForm_ExecuteCallback. Below is the code that you can put on your page after the ScriptManager on the page:
var _WebForm_ExecuteCallback = WebForm_ExecuteCallback;
   
WebForm_ExecuteCallback = function (callbackObject) {
    var response = callbackObject.xmlRequest.responseText;
    var separatorIndex = response.indexOf("|");
   
    if (response.charAt(0) != "s" && response.charAt(0) != "e" && separatorIndex == -1) {
        if ((typeof (callbackObject.errorCallback) != "undefined") && (callbackObject.errorCallback != null))
            callbackObject.errorCallback(callbackObject.xmlRequest.status + "", response);
    }
    else _WebForm_ExecuteCallback(callbackObject);
};

The code simply overrides the original version of the function and raises the same event which it raises when it receives an Exception-response.

RADCOMBOBOX EXAMPLE


Now RadComboBox will alert the status code of the response and will not hang. If you then handle the client-side
ItemsRequestFailed event, you can access the status code and the response from the event arguments and/or cancel the event to suppress the alert and take necessary action, for instance redirect the browser:
function onItemsRequestFailed(sender, eventArgs) {
     var response = eventArgs.get_text();
     var statusCode = eventArgs.get_errorMessage();  
     // Cancel the event so that no alert message appears.
     eventArgs.set_cancel(true);  
     // Redirect...
     window.location = window.location.toString().replace("Default.aspx", "Login.aspx");
}

Note: The event you can use with RadTreeView is NodePopulationFailed. The only difference is that you have access to the status code (again through eventArgs.get_errorMessage()) and not to the response.

You can see how this works in the attached project. 

Comments

There are no comments yet.
If you'd like to comment on this KB article, please, send us a Support Ticket.
Thank you!

Please Sign In to rate this article.