Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am popping up a Custom Application Page using a call to SP.UI.ModalDialog.showModalDialog. Is there a way that I can close the dialog from the code behind after a user clicks a button?

share|improve this question
add comment

3 Answers

up vote 7 down vote accepted

Found a solution. Add this to the end of the button click event:

Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
Context.Response.Flush();
Context.Response.End();
share|improve this answer
add comment

You can also add the SP.UI.ModalDialog.close(SP.UI.DialogResult.OK) event on your button or otherwise make some javascript logic that triggers this event if no errors occured.

Examples 1:

<asp:Button ID="Submit" runat="server" OnClientClick="SP.UI.ModalDialog.close(SP.UI.DialogResult.OK)" />

Example 2:

<script type="text/javascript">
// In case you use jquery ajax for some kind of postback
$.ajax({type: "POST", 
url: some_url, 
data: {},
contentType: ...,
success: function() {SP.UI.ModalDialog.close(SP.UI.DialogResult.OK);},
error: function() {SP.UI.ModalDialog.close(SP.UI.DialogResult.invalid);}
});
</script>
share|improve this answer
add comment

Yes it is possible to close the Modal Dialog from code-behind.

Simply add the below code to your button click event and you are done.

HttpContext context = HttpContext.Current;
            if (HttpContext.Current.Request.QueryString["IsDlg"] != null)
            {
                context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup()</script>");
                context.Response.Flush();
                context.Response.End();
            }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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