Jojo GKH
Jojo GKH

Reputation: 57

Close SP.UI.ModalDialog PopUp from code behind

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 ?

Upvotes: 2

Views: 8969

Answers (3)

Manas Behera
Manas Behera

Reputation: 1

The above code is working fine. It's working perfectly. But to refresh the parent page, use the below code which will work to close the popup and also show the loading message.

using (SPLongOperation operation = new SPLongOperation(this.Page)) {

   operation.Begin();

   //DO WORK HERE

   string redirectUrl = 'http://where-to-redirect-user-after';
     operation.End(redirectUrl, SPRedirectFlags.DoNotEncodeUrl, HttpContext.Current, null);
}

Upvotes: -1

ddev
ddev

Reputation: 389

try

this.Context.Response.Write("<script type='text/javascript'>parent.location.reload();</script>");

Thats what I used on my Code to close the Modal Dialog and also refresh the main page to display the updates made

Upvotes: 0

Joy
Joy

Reputation: 1777

try this :

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

Upvotes: 5

Related Questions