Thursday, October 16, 2014

How to close a layouts page loaded in an SPModalDialog from server side

// In _layouts\YourSolution\dialogpage.aspx:

<asp:Button ID="btnSaveAndClose" runat="server" OnClick="btnSaveAndClose_Click" />


protected void btnSaveAndClose_Click(object sender, EventArgs e)
    // code logic before closing the dialog
    // ..
    // ..
    string script = @"window.frameElement.commonModalDialogClose(1, '');";
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Close", script, true);
    // alternatives for the script variable are:
    // string script = @"window.frameElement.commitPopup();";
    // string script = @"window.frameElement.cancelPopUp();";

    // NOTE: use RegisterStartupScript when executed from .ASCX/webpart:  
    // ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Close", script, true);

A small gotcha I ran into is that I had the btnSaveAndClose control Ajaxified, which caused it to throw exceptions such as:

"0x800a1391 - JavaScript runtime error: 'Sys' is undefined"


"0x800a1391 - JavaScript runtime error: 'ULSSendExceptionImpl' is undefined"

No comments:

Post a Comment