Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a layouts page which creates a new site. I use SP.UI.ModalDialog to show the site. I've created the following js function I use to pop it up:

function PagePopupAndRedir(fileUrl, fileTitle, fileWidth, fileHeight) {
    var options = SP.UI.$create_DialogOptions();
    options.url = fileUrl;
    options.title = fileTitle;
    options.allowMaximize = false;
    options.showClose = true;
    options.width = fileWidth;
    options.height = fileHeight;
    options.dialogReturnValueCallback = modalDialogClosed;

    SP.UI.ModalDialog.showModalDialog(options);
}

the modalDialog method isn't very relevant here, but it starts like this:

function modalDialogClosed(dialogResult, returnValue) {
    if (dialogResult == '1') {
        window.location.href = returnValue;
    }
}

In my layouts page, the last thing I do is:

var response = "<script type='text/javascript'>SP.UI.ModalDialog.commonModalDialogClose(1, 'myUrl')</script>";
Page.Response.Clear();
Page.Response.Write(response);
Page.Response.End();

I can see that the js method is called, but the problem is that the returnValue is never set, it's always undefined. However, returnValue is set in the js-method, but it's not passed based on what I pass, it's passed "directly" from the dialog. I.E. if dialog is correctly closed, it's '1', if it's aborted the value is '0'.

How do I pass parameteres from my server side code to the js-code?

Update: I have observed the funny feature: It SOMETIMES work! About 1 in a few, the parameter is actually passed through to the js-method. Nothing is changed in neither js or cs-code! WTF?

share|improve this question
    
this woked for me sharepoint.stackexchange.com/questions/20473/… –  Nikhil Oct 4 '11 at 6:10

2 Answers 2

Given that it sometimes works, it is probably just a timing issue. Can you try putting it in the onload event and see if it works?

var response = "<script type='text/javascript'>window.onload = function() {SP.UI.ModalDialog.commonModalDialogClose(1, 'myUrl');}</script>";
share|improve this answer
    
I'm sorry to say it doesn't make a difference. –  tarjeieo Oct 3 '11 at 6:43
    
I haven't used the dialog framework to do what you are trying to do, and I'm suspecting that clearing the response is either negating something important, or client script just doesn't have time to run before the dialog is closed. I would recommended just using jQuery to make an ansynch request to the page, and sending the url in the reponse. I have used that approach several times without any problems. –  Jon Lester Oct 3 '11 at 12:40
up vote 3 down vote accepted

I solved the problem / Found the bug.

I neglected to tell you that a SPLongOperation were running inside the modal dialog. I think that might have had an impact, especially since a part of the code were actually closing the dialog after the LongOperation were finished (DOH!). No wonder parameters weren't set.

I still weren't able to write js to the resonse like I'm trying in the serverside code above, but fortunately the SPLongOperation.End() took a script parameter. I sent my "SP.UI.ModalDialog.commonModalDialogClose(1, 'myUrl')" as this parameter, and it worked.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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