4

I have created one custom action. On clicking custom action, I am opening an application page as a modal dialog. The page will allow users to add the data of fields.

I have placed two buttons in the page, 1. Save , 2. Cancel. I want to add such a functionality that if Save is clicked then the form should be closed and the parent page should be refreshed. And if the cancel button is clicked then the page should be closed without refreshing.

I referred this MSDN Link to show the modal dialog.

My code for showing the page as modal dialog is:

var options = {
url:  _spPageContextInfo.webAbsoluteUrl + '/_layouts/15/xx/Pages/Page.aspx',
tite: 'Edit Properties',
allowMaximize: false,
showClose: true,
width: 800,
height: 600,                             
};             
SP.UI.ModalDialog.showModalDialog(options);

My code for closing the page without refreshing the page is:

protected void btnCancel_Click(object sender, EventArgs e)
{
    HttpContext context = HttpContext.Current;
    Regex regEx = new Regex("[?&]IsDlg=1");
    string currentUrl = Page.Request.RawUrl;
    if (regEx.IsMatch(currentUrl))
    {
        context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
        context.Response.Flush();
        context.Response.End();
    }
}

When I click on the cancel button I am getting the following error in the console of the browser.

Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.

What am I missing?

5
  • How do you initialize Context?
    – Jussi Palo
    Commented May 22, 2015 at 6:53
  • I haven't defined the Context object. It is Page.Context which is accessible directly.
    – users1100
    Commented May 22, 2015 at 8:38
  • Can you try with HttpContext context = HttpContext.Current; ?
    – Jussi Palo
    Commented May 22, 2015 at 8:47
  • Yes sir, I tried that way also, (I have edited code in the question after changes as per your suggestion) but not working.
    – users1100
    Commented May 22, 2015 at 8:55
  • Now I remember running into this same issue some time ago. Eventually I decided to remove the Cancel button and rely on user clicking on the top right X to close the dialog, so sorry, I will need to leave this for someone else to solve. Good luck!
    – Jussi Palo
    Commented May 22, 2015 at 9:01

3 Answers 3

1

In the options add

dialogReturnValueCallback: DialogCallback

and add the callback function where you refresh the page when you want.

The complete code:

var options = {
   url:  _spPageContextInfo.webAbsoluteUrl + '/_layouts/15/xx/Pages/Page.aspx',
   tite: 'Edit Properties',
   allowMaximize: false,
   showClose: true,
   width: 800,
   height: 600,  
   dialogReturnValueCallback: DialogCallback                          
};             
SP.UI.ModalDialog.showModalDialog(options);


DialogCallback = function (result, returnValue) {
    if (result == SP.UI.DialogResult.OK) {
        //uncomment the row blow to refresh the page
        //SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
    }
}

Update:

Apart from that I use my own buttons and usually use SPLongOperation

protected void yourButton_Click(object sender, EventArgs e)
    {
        try
        {
            string returnURL = "";//your return url
            string endscript = "try{window.frameElement.commonModalDialogClose(0, 0);}catch (error){window.location.href = '" + returnURL + "';}";
            using (SPLongOperation ctx = new SPLongOperation(this.Page))
            {
                ctx.TrailingHTML = "";
                ctx.LeadingHTML = "";
                ctx.Begin();



                ctx.EndScript(endscript);
            }

        }
        catch (ThreadAbortException ex)
        {
        }
        catch (Exception ex)
        {
            throw;
        }
    }
6
  • Refreshing the page will definitely get rid of the dialog, but OP wanted to close the dialog without parent page refresh.
    – Jussi Palo
    Commented May 22, 2015 at 9:02
  • @JussiPalo That's why I comment the last line. Without that line it will close the dialog and not refresh the parent page. Commented May 22, 2015 at 9:06
  • I tried your suggested way but not worked. @Naim
    – users1100
    Commented May 22, 2015 at 9:07
  • @users1100 can you try in your code with window.frameElement.commonModalDialogClose(0, 0); instead of window.frameElement.commitPopup(); Commented May 22, 2015 at 9:28
  • Hello sir, I tried your suggested way also but didn't help :-( @Naim
    – users1100
    Commented May 22, 2015 at 10:25
0

Try

Context.Response.Clear()

before writing anything to it. This would ensure that ONLY your javascript is in the response. otherwise you could deliver a malformed html-page.

0

What has worked for me is to create a blank page with script on the page using a content editor webpart. I am using the fancybox jQuery plugin but I get a similar behavior when I try to close the box through the Save button.

On my blank page, as I have mentioned earlier, I added a content editor webpart and put my script to close the box. Something along the lines of:
<script type="text/javascript">window.frameElement.commitPopup();window.close();</script>

On your Save button, the action is to just redirect to your new blank page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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