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,
                dialogReturnValueCallback: onPopUpCloseCallBackWithData
            modalDialog = SP.UI.ModalDialog.showModalDialog(options); 
        function onPopUpCloseCallBackWithData(result, returnValue){
            document.getElementById('"+ tbUrl.ClientID +"').value = returnValue;

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, 2014 at 8:32

1 Answer 1


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()
    function modalClosesd(result, args) {
        if (result == SP.UI.DialogResult.OK) {
            $('#<%= tbArg1.ClientID %>').val(args.arg1);
            $('#<%= tbArg2.ClientID %>').val(args.arg2);

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");

The server side Click event handler:

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

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.

  • thanks for your proposition but it still doesn't work
    – bender
    Feb 19, 2014 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. Feb 19, 2014 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, 2014 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. Feb 19, 2014 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, 2014 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.