Close SP.UI.ModalDialog PopUp from code behind

Jojo GKH picture Jojo GKH · Nov 23, 2012 · Viewed 8.8k times · Source

I have a list of Items on an Aspx Page and in order to edit each item I have launched SP.UI.ModalDialog popup to do so, a submit buton is created in this PopUp window to save changes and then close the pop up window :

protected void Submit_onclick(Object sender, EventArgs e)
{
    SPSecurity.RunWithElevatedPrivileges(delegate
    {
        try
        {
            using (SPSite mySite = new SPSite(PGContext.Site.ID))
            {
                using (SPWeb myweb = mySite.OpenWeb(PGContext.Web.ID))
                {
                    myweb.AllowUnsafeUpdates = true;
                    //changes
                    myweb.AllowUnsafeUpdates = false;

                    Page.ClientScript.RegisterStartupScript(this.GetType(), "closeWindow", "window.close()", true);}
                }
            }
            catch (Exception exp)
            {
                throw new Exception("ERROR: Unable to Save Changes : " + exp.Message.ToString(), exp);
            }
        });
}

but the Page.ClientScript.RegisterStartupScript() doesn't seem to work ! I've looked at it at msdn and it says that i can only use it on Page_Load() fonction , so how can I close my popup from the code in the event ?

Answer

Joy picture Joy · Nov 23, 2012

try this :

Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
Response.Flush();
Response.End();