Thursday, February 9, 2012

Close the pop-up window in ASP.NET


The following C# code defines the JavaScript code for closing popup window, and registers it to execute immediately upon the page being posted back the next time.

/// Closes the pop-up window.
public void CloseWindow()
{
string strClose = "window.opener=''; window.close();";
//Registers the startup script with the Page object.
ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), strClose, true);
}



// Closes the window and refresh opener page.
public void CloseWindowAndRefreshOpener()

{
string strClose = "if(window.opener) { window.opener.location.href = window.opener.location.href;} " +
"else { if(dialogArguments) {dialogArguments.location.href = dialogArguments.location.href; " +

"} } self.close();";
ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), strClose, true);
}

// Closes the window and refresh opener parent.
public void CloseWindowAndRefreshOpenerParent()
{
string strClose = "if(window.opener) { window.opener.parent.location.href = " +
"window.opener.parent.location.href;} else { if(dialogArguments) " +
"{dialogArguments.parent.location.href = dialogArguments.parent.location.href; " +
"} } self.close();";
ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), strClose, true);
}

No comments:

Post a Comment

Open default email app in .NET MAUI

Sample Code:  if (Email.Default.IsComposeSupported) {     string subject = "Hello!";     string body = "Excellent!";    ...