2

I'm using a ModalDialog and have a strange issue.

On a button click server side in my popup, I've this line:

this.Page.ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", "popData = '" + checkedValue + "';SP.UI.ModalDialog.commonModalDialogClose(1, popData);", true);

Here is what I have in the parent:

LiteralControl lc = new LiteralControl();
lc.Text = @"
    <script language='javascript' type='text/javascript'>
        function ShowDialog(){
            var modalDialog;
            var options = {
                title: 'Vue détaillée',
                allowMaximize: true,
                showClose: true,
                url:'/_Layouts/Molnlycke.search/PopupSelUrl.aspx',
                dialogReturnValueCallback: onPopUpCloseCallBackWithData
            }; 
            modalDialog = SP.UI.ModalDialog.showModalDialog(options); 
        }
        function onPopUpCloseCallBackWithData(result, returnValue){
            document.getElementById('"+ tbUrl.ClientID +"').value = returnValue;
        }
    </script>";

this.Page.ClientScript.RegisterStartupScript(this.GetType(), "JS", lc.Text);

and my button:

<asp:Button ID="btSelUrl" runat="server" OnClientClick="ShowDialog()" Text="Open popup"/>

I correctly receive the value in the parent windows, but it doesn't close the popup.

If I click a second time, it closes the popup.

Do you know what the mistake is?

  • i removed the onclientclick="showdialog()" and place in page_load: btSelUrl.Attributes["onclick"] = "ShowDialog();return false;"; now it seems working – bender Feb 20 '14 at 8:32
2

Sounds like your timing is getting crossed. Here is a full working example of using the modal dialog, and passing data to and from the parent page:

In the parent page mark up:

<script type="text/javascript">
    function showModal() {
        var options = {
            url: '/web/_layouts/Dialog/TestModal.aspx',
            title: 'Hello World',
            allowMaximize: false,
            dialogReturnValueCallback: modalClosed,
            args: {
                arg1: $('#<%= tbArg1.ClientID %>').val(),
                arg2: $('#<%= tbArg2.ClientID %>').val()
            }
        };
        SP.UI.ModalDialog.showModalDialog(options);
    }
    function modalClosesd(result, args) {
        if (result == SP.UI.DialogResult.OK) {
            $('#<%= tbArg1.ClientID %>').val(args.arg1);
            $('#<%= tbArg2.ClientID %>').val(args.arg2);
        }
    }
</script>

In your modal dialog markup:

<script type="text/javascript">
    function cancelClicked() {
        SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.Cancel, null);
    }
    ExecuteOrDelayUntilScriptLoaded(function () {
        var args = SP.UI.ModalDialog.get_childDialog().get_args();
        if (!isNullOrEmpty(args.arg1)) {
            $('#<%= tbArg1.ClientID %>').val(args.arg1);
        }
        if (!isNullOrEmpty(args.arg2)) {
            $('#<%= tbArg2.ClientID %>').val(args.arg2);
        }
    }, "SP.UI.Dialog.js");
</script>

The server side Click event handler:

protected void btnSave_Click(object sender, EventArgs e)
{
    Page.ClientScript.RegisterStartupScript(Page.GetType(), "saveSuccess",
        @"<script type='text/javascript'>
            ExecuteOrDelayUntilScriptLoaded(
                function() {
                    var args = {
                        arg1: '" + tbArg1.Text + @"',
                        arg2: '" + tbArg2.Text + @"'
                    };
                    SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK, args);
                }, 'SP.UI.Dialog.js');
        </script>");
}

The ExecuteOrDelayUntilScriptLoaded() function will cause the page to wait to fire the delegate passed to it until after the SharePoint 'SP.UI.Dialog.js' javascript file (containing the SP.UI.ModalDialog.commonModalDialogClose function) has been loaded.

| improve this answer | |
  • thanks for your proposition but it still doesn't work – bender Feb 19 '14 at 15:28
  • I pulled this code from a page I've used it in. And just tested to make sure. Are you seeing any errors in the developer tools console? I know you didn't declare "popData" as "var popData" in the code you posted, wonder if you're getting a different js error. – RJ Cuthbertson Feb 19 '14 at 15:48
  • i have no error in developper tool. i declare popData in page_load with: Page.ClientScript.RegisterStartupScript When i click, i see the popup reloaded and at the second time it closes. thanks – bender Feb 19 '14 at 16:32
  • I meant in your code that you posted, you wrote "popData = '" + checkedValue ... instead of var popData. I understand what you're saying about the dialog, but I also know for a fact that you can close the dialog in a button click handler using the code I posted for you, because it's code I've used in an application, and I just tested it again. Good luck. – RJ Cuthbertson Feb 19 '14 at 16:40
  • i added var before popData but doesn't change anything. doesn't it come from the call of my popup? Do you have a complete exemple for opening and retrieve value from popup like i use here? thanks – bender Feb 19 '14 at 20:33

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.