Handling OnAjaxRequest in Content Pages with Ajax Proxy

Wednesday, February 25, 2009 by Todd Anglin | Comments 9

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

9 Comments

  • Greg C. 01 Mar 2009
    Thank you, this was helpful.
  • blazed & confused 03 Jun 2009
    thanks, very to the point and what i needed.. u should write examples for telerik.
  • p 07 Sep 2009
    Brilliant! Thanks
  • Sunil 30 Nov 2009
    Hi Todd,

     I'm working on VB and would like to know the VB part of the code you provide.
    What would be the equivalient VB , for nthe code below :

     var manager = RadAjaxManager.GetCurrent(this); 

    Thanks
  • ulu 04 Dec 2009
    Should be

    Dim manager = RadAjaxManager.GetCurrent(Me)
  • Dimitri Stoikof 26 Sep 2010
    Hi Todd,

    Thanks for your post, it confirms what i've managed to figure out by trial and error myself.

    Your solution works fine for one usercontrol, but what if there are multiple usercontrols making ajaxRequest's? (or multiple contentpages for that matter)

    I've started a thread with Telerik to get help on this, but i've got a creeping feeling that the telerik developers are very aware of this limitation but don't have a solution to it, since it seems like the issue is being side-stepped in a number of forum questions.

    Have you got any suggestions on this?

    Thanks,
    Dimitri Stoikof
    Insidelogic
  • Hassan Gulzar 28 Oct 2010
    Thank you for the excellent post. helped me a lot.
  • Sloan 19 Jan 2011
    perfect, thanks T
  • MOHAN DEVAL 24 Jan 2012
    The specific error I get is on the line 
    'Create a new delegate to handle the AjaxRequest  
            manager.AjaxRequest += New RadAjaxControl.AjaxRequestDelegate(YourContentPage_AjaxRequest)
    where it tells me to use AddressOf.....
     Dim manager = RadAjaxManager.GetCurrent(Me)
    as ... user control cannot be converted to system.web.ui.page.
    A third place where I get the error is 
     manager.AjaxSettings.AddAjaxSetting(manager, Me.controlToUpdate)
    Please Help-------
    --------------------The VB.Net code----------------- 
    Protected Sub Page_Load(sender As Object, e As EventArgs)
    'Get reference to AjaxManager (from Master) 
    Dim manager = RadAjaxManager.GetCurrent(Me)
    '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, Me.controlToUpdate)
    End Sub
    'Handle the Ajax event in your ContentPage code behind 
    Private Sub YourContentPage_AjaxRequest(sender As Object, e As AjaxRequestEventArgs)
    'Optionally process supplied event arg 
    Select Case e.Argument
    Case "SomeEventArgument"
    'Do something 
    Exit Select
    End Select
    End Sub

Add comment

  1. Formatting options
       
     
     
     
     
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)