This is a migrated thread and some comments may be shown as answers.

ajaxRequestWithTarget within UserControl

9 Answers 661 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Jeff
Top achievements
Rank 1
Jeff asked on 31 Mar 2008, 08:24 PM
I'm struggling to find the right way to do this.

I have a user control with a RadGrid and an Edit button in it.  Clicking the Edit button retrieves the primary key for the selected row within the RadGrid and opens a RadWindow passing the primary key to the page opened in the window using a Query String.

Then I have a javascript routine that handles the Window close and does a Rebind on the RadGrid to refresh it with the latest values.  I use the argument from the Window to get the primary key again and select the corresponding row in the RadGrid.  This is done by calling 

var

ajaxManager = $find("<%= this.Page.AjaxManager.ClientID %>");
ajaxManager.ajaxRequestWithTarget('<%= Button_EditEntry.UniqueID %>', oWnd.argument);

The trouble is, this works fine within a page, but within a UserControl I seem to be faced with a dilema:

To retrieve the argument passed in the Ajax Request, I have to implement a RaisePostBackEvent method in my code-behind.  For a page, this is a simple matter of overriding the page's implementation of this.  For a UserControl, I have to implement IPostBackEventHandler.  According to the documentation, to ensure that my RaisePostBackEvent function gets called, I have to call ajaxRequestWithTarget using the UniqueID of the UserControl itself instead of the button, but if I do that, all the controls within the UserControl get updated during the Ajax Request instead of just those controls that Button_EditEntry should affect.

Within a UserControl, how can I initiate an Ajax call on the client side, retrieve the argument on the server side, and limit the controls that are updated to one or two instead of the entire UserControl?

Jeff

9 Answers, 1 is accepted

Sort by
0
Jeff
Top achievements
Rank 1
answered on 02 Apr 2008, 02:46 PM
Any ideas?  Using ajaxRequestWithTarget within a user control, is there any way to similuate a button click, etc.?
0
Steve
Telerik team
answered on 03 Apr 2008, 03:21 PM
Hello Jeffrey,

You can't do this, because it seems that the MS guys have forgotten that you could have a scenario where a component that does not render any html can actually submit the page. This way if you specify the user control as initiator it would throw "Sys.ArgumentNullException: Value cannot be null. Parameter name: postBackElement".
To avoid that you could ignore RaisePostBackEvent and use the AjaxManager to handle this scenario like that:

  protected void Page_Load(object sender, EventArgs e)
    {
        RadAjaxManager ajaxManager = RadAjaxManager.GetCurrent(Page);
        ajaxManager.AjaxRequest += new RadAjaxControl.AjaxRequestDelegate(ajaxManager_AjaxRequest);
    }

    void ajaxManager_AjaxRequest(object sender, AjaxRequestEventArgs e)
    {
        ScriptManager.GetCurrent(Page).AsyncPostBackSourceElementID; //you get the initiator here or in the e.Args where you have passed the arg as part of the AjaxRequest()
    }

All the best,
Steve
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Jeff
Top achievements
Rank 1
answered on 10 Apr 2008, 04:38 PM
That did the trick.  Thanks again :)
0
Kjartan
Top achievements
Rank 1
answered on 18 Sep 2008, 08:28 AM
Hi,

I'm faced with the same dilemma.
In a usercontrol:

If I have a contextmenu on a treeview, and then when a menuitem is clicked, i display a radprompt, the user enters a value and I initiate an ajaxrequest to the server with the foldernode. How would i update the treeview on the ajax_request event? If i add a arbitrary node it doesn't get reflected in the treeview, even though i have a radajaxmanagerproxy with an ajaxsetting with treeview.
0
Maria Ilieva
Telerik team
answered on 18 Sep 2008, 10:41 AM
Hello Kjartan,

You could suggest using the RadAjaxManager.GetCurrent(Page) method in order to get the manager and ajaxify the TreeView in the UC instead of using RadAjaxManagerProxy control. In this way you will also be able to use the RadAjaxManager functionality (like ResponseScripts collection, ajaxRequest function). More information on similar scenario could be found in this help topic.

All the best,
Maria Ilieva
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Kjartan
Top achievements
Rank 1
answered on 18 Sep 2008, 11:06 AM
Hi,

I think you misunderstood my question. I have the exact same scenario as the guy who started this post, and I'm already using the approach Steve from telerik suggested. However,

in the following method:

void ajaxManager_AjaxRequest(object sender, AjaxRequestEventArgs e)
    {
        ScriptManager.GetCurrent(Page).AsyncPostBackSourceElementID;

       MyTreeView.Nodes.Add(new RadTreeNode("test", "test"));
    }

If i add a node like the above method, my tree is not updated with the changes when the request finishes. (remember that this is in a usercontrol like the example in this thread).

Thanks
0
Maria Ilieva
Telerik team
answered on 19 Sep 2008, 01:50 PM
Hi Kjartan,

Will it be convenient for you to open a regular support ticket and send us small runnable application, which replicates the described behaviour? We will test it locally and do our best to provided accurate solution.

Greetings,
Maria Ilieva
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Rebecca Campbell
Top achievements
Rank 1
answered on 09 Apr 2010, 06:10 PM
I'm having a little trouble with this, perhaps you can help.  My delegate is not called when I use ajaxRequestWithTarget(), but is called when I just use ajaxRequest().  Here's the code I'm working with:

C#
protected void Page_Load(object sender, EventArgs e)
{
     RadAjaxManager manager = RadAjaxManager.GetCurrent(Page);
     manager.AjaxRequest += new RadAjaxControl.AjaxRequestDelegate(ajaxManager_AjaxRequest);
}

public void ajaxManager_AjaxRequest(object sender, AjaxRequestEventArgs e)
{
     calendarPopup.ShowStuff(e.Argument); //call method on user control
}

JavaScript
$find('<%# RadAjaxManager.GetCurrent(Page).ClientID %>').ajaxRequestWithTarget('<%# calendarPopupContainer.ClientID %>', s);
//$find('<%# RadAjaxManager.GetCurrent(Page).ClientID %>').ajaxRequest(s);

When I use the first JavaScript with ajaxRequestWithTarget(), the content area in question is re-drawn (I'm printing out the Time and it changes whenever it reloads), but the AjaxRequestDelegate never executes in this case.

If I use ajaxRequest(), the AjaxRequestDelegate is executed, but my content area isn't re-drawn.

Help?

Thanks in advance!
Rebecca
0
Rebecca Campbell
Top achievements
Rank 1
answered on 09 Apr 2010, 06:28 PM
Please disregard my previous post.  It seems to be working now.
Tags
Ajax
Asked by
Jeff
Top achievements
Rank 1
Answers by
Jeff
Top achievements
Rank 1
Steve
Telerik team
Kjartan
Top achievements
Rank 1
Maria Ilieva
Telerik team
Rebecca Campbell
Top achievements
Rank 1
Share this question
or