5

I have a custom page that I load in a SharePoint dialog. I call the javascript from an ASPX page, but when I close the dialog I want the javascript callback to "push" a value back to my ASPX page. Is this possible?

8

Yes, certainly it's possible and can be done through dialogReturnValueCallback parameter.

Sample code for popping up the dialog:

var options =
    {
        url: '/_layouts/CustomDialogPage.aspx',
        title: 'My modal dialog',
        dialogReturnValueCallback:
            function (dialogResult, returnValue) {
                if (dialogResult == SP.UI.DialogResult.OK) {
                    alert(returnValue);
                }
            }
    };
SP.UI.ModalDialog.showModalDialog(options);

... and for returning the value (from CustomDialogPage.aspx):

window.frameElement.commitPopup('Hello from custom dialog page!');

If you're using postbacks and server-side code in your custom dialog page, you should use the following code to achieve this:

Response.Write("<script type='text/javascript'>window.frameElement.commitPopup('Hello from custom dialog page!');</script>");
Response.Flush();
Response.End();
  • it's the other way around: when I close my dialog, I want to sent back a value to the javascript caller(Dialog->callback->aspx) who call the showModalDialog. – Gabriel Mongeon Nov 2 '11 at 20:37
  • So you want execution to continue after the call to showModalDialog, and use the returned value, right? Just move your code into the callback. Javascript can't wait synchronously, it is completely asynchronous by design. – Andrey Markeev Nov 2 '11 at 20:46
-2

Here's what I've done:

function dialogCallback(result, value) {
    if (result === SP.UI.DialogResult.OK) {
        // find the control to set the value
        var obj = document.getElementById(getRealId('MY_REALLY_UNIQUE_ID'));

        if (obj != null) {
            obj.value = value;
        }
    }
}
// Get the complete id of the control
function getRealId(partialid) {
    var re = new RegExp(partialid, 'g');
    var elems = document.getElementsByTagName('*'), i = 0, el;
    while (el = elems[i++]) {
        if (el.id.match(re)) {
            return el.id;
        }
    }
}

It's was more an javascript thing than a Sharepoint one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.