0

I have created a button and on the click of that button a JavaScript code gets triggered. I have given the link of a list of SharePoint that gets opened as a modal dialog box on the click of that button. I am facing a problem in opening that modal popup. Whenever I click on the button the modal popup opens but only for a fractions of seconds and then gets closed automatically.

Please suggest a way to display that modal dialog box.

404
  • 2,215
  • 4
  • 15
  • 39
user39250
  • 11
  • 1
  • 4

2 Answers2

1

When you have used Asp:Button, it is doing postback. Hence the opened dialog model is closed. You can avoid the postback event by adding "return false" after the method call in the OnClientClick event. It worked fine for me.

<asp:Button ID="btnJustify" runat="server" Text="Justify" OnClientClick="Justify();return false" />

In order to update the details through modal, the item Id needs to be passed. Seems you are not passing the Id parameter in your code. Below code snippet works for your scenario.

<script type="text/javascript">
    function Justify() { var options = SP.UI.$create_DialogOptions(); options.width = 500; options.height = 500; 
    options.url = "/sites/SAMS/Lists/DownloadJustify/EditForm.aspx?ID=1"; options.dialogReturnValueCallback = Function.createDelegate(null, dialogReturnValueCallback); options.showClose = true; SP.UI.ModalDialog.showModalDialog(options); }
    function dialogReturnValueCallback(res, retVal) {
        if (res === SP.UI.DialogResult.OK) {
            alert('success');
        }
        else {
            alert('failed');
        }
    }
</script>

Hope this helps to resolve your issue.

0

Solution1: SP.UI.ModalDialog class from javascript and use _spBodyOnLoadFunctionNames to call it on Page Load

        <script language="javascript" type='text/javascript'>
    function openModelDialogPopup(strPageURL ) {

    var dialogOptions = {
        title: "This is Modal Dialog", //Popup title.
        url: strPageURL, 
        width: 600, // Width of the dialog.
        height: 400
    };
    SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.showModalDialog', dialogOptions);
    return false;
}      
    </script>
Robert Lindgren
  • 24,520
  • 12
  • 53
  • 79
Nagaraju Pattem
  • 1,020
  • 1
  • 15
  • 21
  • Thanks Nagaraju. But I am still getting the same problem – user39250 Feb 23 '15 at 07:14
  • can you share code and where your are created that button, it means which page? – Nagaraju Pattem Feb 23 '15 at 07:18
  • In VisualWebpartUserControl.ascx I have created a button as justify : <asp:Button ID="btnJustify" runat="server" Text="Justify" OnClientClick="Justify()" /> – user39250 Feb 23 '15 at 07:23
  • function Justify() { var options = SP.UI.$create_DialogOptions(); options.width = 500; options.height = 500; options.url = "/sites/SAMS/Lists/DownloadJustify/EditForm.aspx"; options.dialogReturnValueCallback = Function.createDelegate(null, success); options.showClose = true; SP.UI.ModalDialog.showModalDialog(options); } – user39250 Feb 23 '15 at 07:25
  • are you following this post http://sharepoint.stackexchange.com/questions/38405/sharepoint-modal-help-pass-value-back-to-parent-when-closing – Nagaraju Pattem Feb 23 '15 at 07:38
  • Replace asp:Button to linkbutton then it will work.. try this one http://stackoverflow.com/questions/9329865/cant-prevent-modaldialog-to-close-in-sharepoint-web-part – Nagaraju Pattem Feb 23 '15 at 07:41
  • Thanks for your help. I used Html button ... now it is working.... Modal dialog box is getting displayed. But when I click on SAVE button, i get this error 'The server was unable to save the form at this time. Please try again.' Any idea?? – user39250 Feb 23 '15 at 10:50