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

RadWindow callback function executes before the window

4 Answers 458 Views
Window
This is a migrated thread and some comments may be shown as answers.
Massimiliano
Top achievements
Rank 1
Massimiliano asked on 21 May 2013, 02:41 PM
I'm trying to show an alert before a page redirect (after a form submission).
I tryed every possible scenario but it seems that the callback function (wich actually performs the redirect) is always executed before the window.

As a last option I tryed
<asp:Panel ID="AjaxPanel" runat="server" defaultbutton="LoginConfirm">
    <telerik:RadScriptBlock ID="RadScriptBlock" runat="server">
        <script type="text/javascript">
            activeAjaxRequest = false;
            noPermanentLogin('sdf');
        </script>
    </telerik:RadScriptBlock>
where noPermanentLogin just:
function noPermanentLogin(args) {
    radalert('Radalert is called from the client!', 330, 180, 'Client RadAlert', alertCallBackFn(args), '');
}
function alertCallBackFn(args) {
    alert(args);
    //window.location.href = args
}

Now when I first load the page the RadWindow should open each time and upon clicking "OK" button the alert should fire.
Instead the alert fires and the window is not created because of:
Uncaught TypeError: Cannot call method 'radalert' of undefined
It seems that the radwindow object has not yet been instantiated but nonetheless the callbackfunction executes the same.. this is quite mindblowing to me...

In the real scenario I tryed several versions, whose behaviour (in all situations) is that the callbackfunction gets executed BEFORE the window is showed. In those cases btw the window is showed.
So I tryed in code behind:
Me.AjaxManager.ResponseScripts.Add("noPermanentLogin('returnUrl')")
Then I tryed
RadWindowManager.RadAlert("Testina", Nothing, Nothing, "Totolo", "window.location.href = '" & returnUrl & "'")
And finally I tryed:
RadWindowManager.RadAlert("Testina", Nothing, Nothing, "Totolo", "redirectFunction('testurl')")
(in this last case redirectFunction calls the window redirect and is in an external JS in the page).

Why the callback function is executed even before showing the radalert?
Thanks in advance for your help




4 Answers, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 23 May 2013, 03:00 PM
Hello Massimiliano,

The callback function of the RadAlert is a function object, i.e. you need to pass its name. This is explained and shown in the following places:
http://www.telerik.com/help/aspnet-ajax/window-dialogs-alert.html
http://demos.telerik.com/aspnet-ajax/window/examples/browserdialogboxes/defaultcs.aspx

Adding the parentheses causes the function to execute immediately as this is their purpose.

What you need is:
radalert('Radalert is called from the client!', 330, 180, 'Client RadAlert', alertCallBackFn, '');

The error that you get is because the script is called from the code-behind and it is executed too early, you need to use the Sys.Application.Load event, as shown here and here, or you can simply use the server-side RadAlert() method you can see in the above demo.


Regards,
Marin Bratanov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Massimiliano
Top achievements
Rank 1
answered on 23 May 2013, 06:23 PM
Thank you Marin tomorrow I'll check your suggestions. I think I got the parenthesis stuff... the "args" inside it is the action executed on the alert windows so I cannot pass "args" to the function in the radalert call otherwise is like I'm providing the action already.
I should find another way to pass back the url variable then during ajax, maybe a global js variable or such.

Thanks again you  are precious as always.
0
Travis Cotton
Top achievements
Rank 1
answered on 28 Mar 2014, 08:01 PM
This is a pretty old post but for anyone else who has this same issue the solution I found is this:

In the page load method register the script you want to execute and at that time set the URL you want the system to redirect to when the alert is closed:

        Page.ClientScript.RegisterStartupScript(Me.GetType, "callBackFunction", "<script> function callBackFunction(arg) { window.location.href ='/someurl.aspx' } </script>")

Now you can call the RadAlert like it was intended:

                    RadWindowManager1.RadAlert("<h1>Your Alert Heading</h1><p>Some message here</p>", 500, 500, "Notification", "callBackFunction")

Works like a charm!




0
Enrico Ariel
Top achievements
Rank 2
answered on 03 May 2016, 12:43 PM

this is how I solved the same issue:

On server side:

string newId = "myParameter";
string callBackFunction = String.Format("function(){{onSaveNewClientCallBackFunction('{0}');}}", newId);
RadWindowManager1.RadAlert("New Item Added Successfully", 250, 250, "Message",
callBackFunction);

and on client side:

function onSaveNewClientCallBackFunction(id) {
   alert(id);
}

I hope it can be useful.

Tags
Window
Asked by
Massimiliano
Top achievements
Rank 1
Answers by
Marin Bratanov
Telerik team
Massimiliano
Top achievements
Rank 1
Travis Cotton
Top achievements
Rank 1
Enrico Ariel
Top achievements
Rank 2
Share this question
or