0
votes

I am calling SP.UI.ModalDialog.showModalDialog() in a javascript function of my Web Part (SharePoint 2010) but the dialog appears for one second, then closes itself.
Originally, I wanted to call a sharepoint page (in Layouts folder) but I am testing with a simple dialog to make sure it isn't an error in my sharepoint page.

In the Web Part, I have a table where each first cell row is a LinkButton. I have set the OnClientClick to call a javascript function :

linkButton.OnClientClick = string.Format("OpenNotationCurveDialog('{0}');",
                    notation.code);

In the script block of the web part ASCX, I have :

function OpenNotationCurveDialog(notationCode) {

    var htmlElement = document.createElement('p');

    var helloWorldNode = document.createTextNode('Hello world!');
    htmlElement.appendChild(helloWorldNode);

    var options = {

        title: "Add item",
        allowMaximize: true,
        showClose: true,
        width: 800,
        height: 600,
        html: htmlElement,
        dialogReturnValueCallback: CurveDialog_ClosedCallback

    };

    SP.UI.ModalDialog.showModalDialog(options);

    return false;
}

function CurveDialog_ClosedCallback(result, value) {
    console.log('!! CurveDialog_ClosedCallback !!');
}

The function CurveDialog_ClosedCallback is never called. In the same Web Part, I have added an element to the Web Part menu that calls the javascript function SP.UI.ModalDialog.showModalDialog() : it is working perfectly. I don't understand why it is working when calling showModalDialog() from the menu and not working from the LinkButton client event.

Any idea how to solve this very annoying behavior ? thx

1

1 Answers

1
votes

What probably happens is that the linkbutton performs a postback so that the page reloads, causing the dialog to disappear Try using a simple anchor-tag instead.