Here’s a quick tip for those of you using RadAjaxManager and RadAjaxManagerProxy. The scenario:

 

You have a MasterPage with a RadAjaxManager and a ContentPage with a RadAjaxManagerProxy (so far, so good, and you’re following Telerik’s best practice guidance). All declarative Ajax settings work fine with the Proxy- no trouble. But now you want fire an Ajax event on your ContentPage manually using JavaScript and handle the AjaxRequest event on the server. You’re shocked to discover there is no “AjaxRequest” event for the RadAjaxManagerProxy! What’s the solution?

 

Fortunately, the solution is easy. The RadAjaxManagerProxy is designed to make it easy to configure Ajax settings in complex MasterPage, ContentPage, and UserControl scenarios, but it doesn’t expose the events that the “parent” RadAjaxManager exposes. To use those events on a content page, you must simply get a reference to the parent RadAjaxManager (via a handy static helper method) and then setup your “manual” Ajax setting in code, like this:

C# (in your ContentPage with the Proxy)

//In your ContentPage OnPageLoad 
protected void Page_Load(object sender, EventArgs e) 
{ 
 //Get reference to AjaxManager (from Master) 
    var manager = RadAjaxManager.GetCurrent(this); 
 
 //Create a new delegate to handle the AjaxRequest event 
    manager.AjaxRequest += new RadAjaxControl.AjaxRequestDelegate(YourContentPage_AjaxRequest); 
 
 //Add your ajax settings programmatically (with ref to Master manager) 
    manager.AjaxSettings.AddAjaxSetting(manager, this.controlToUpdate); 
} 
 
//Handle the Ajax event in your ContentPage code behind 
private void YourContentPage_AjaxRequest(object sender, AjaxRequestEventArgs e) 
{ 
 //Optionally process supplied event arg 
 switch (e.Argument) 
    { 
 case "SomeEventArgument": 
 //Do something 
 break; 
    } 
} 

With this code in place, you can handle your OnAjaxRequest server-side event in your ContentPage code-behind when the Ajax event is execute from your JavaScript code, like this:

JavaScript (in your ContentPage with Proxy)

function someJavaScriptFunction() { 
 //Get reference to RadAjaxManager on page 
 if (radManager == null) 
       radManager = $find('<%= RadAjaxManager.GetCurrent(this).ClientID %>'); 
 
 //Fire ajax request (optionally pass an event arg value) 
   radManager.ajaxRequest("SomeEventArgValue"); 
} 

Just that simple! The key to both the JavaScript and C# code is the static “RadAjaxManager.GetCurrent()” method, which quickly gives you a reference to the “top level” RadAjaxManager in a ContentPage or UserControl. With that method in hand, you can easily tackle advanced scenarios with the RadAjaxManager and the Proxy control.

Check out more, including how to handle OnRequestStart and OnRequestEnd client-side events on ContentPages, in the RadAjax docs


About the Author

Todd Anglin

is an avid HTML5, CSS3, and JavaScript advocate, and geek about all things web development. He is an active speaker and author, helping developers around the world learn and adopt HTML5. Todd works for Telerik as VP of HTML5 Web & Mobile Tools, where his current technical focus is on Kendo UI. Todd is @toddanglin on Twitter

Related Posts

Comments