C# .NET - how to forcefully close browser window using js/c#

Asked By john sat on 30-Jun-11 10:37 AM
Hi ,
I am working in c#.
I am opening a open/saveas dialog box at the load event, and after the dialog box open, how would i close the browser. i no need the browser tobe open.
ie:After the dialog box open, the browser should be closed automatically.

Thanks

Jitendra Faye replied to john sat on 30-Jun-11 10:43 AM
Use this code to Clode Browser-

  //FOR CLOSING BROWSER

       string script = "window.opener = 'Self';window.open('','_parent',''); window.close();";
      Page.ClientScript.RegisterStartupScript(this.GetType(), "Close Window", scr, false);

TRy this and let me me know.

    

Ravi S replied to john sat on 30-Jun-11 10:43 AM
hi

If you've ever tried to close a browser window from ASP.NET by using the following code:


<input type="button" class="inputfields"
 onclick="javascript:window.close();" value="Close" /></td>

You will probably receive the following message:

http://blogs.infinite-x.net/images/JavascriptHowtoClose.closeWithoutWarning_D055/image06.png


After digging around for 20 minutes or so, I finally found the answer on Anatoly Lubarsky's http://blogs.x2line.com/al/articles/350.aspx:


<input type="button" class="inputfields"
onclick="javascript:window.opener='x';window.close();" value="Close" /></td>
Riley K replied to john sat on 30-Jun-11 10:44 AM
To close a parent window from a Child window simply write this

window.opener.close();

It will help

Thanks
john sat replied to Jitendra Faye on 30-Jun-11 10:49 AM
Hi,
Thanks for your replay..
Is any thing tobe included for
Page.ClientScript.RegisterStartupScript(this.GetType(), "Close Window", scr, false);
this line
Jitendra Faye replied to john sat on 30-Jun-11 11:07 AM
No need to add anything extra ,

Wrtite this code in .cs file and call this code when you want to close browser.

Hope this will help you.
Devil Scorpio replied to john sat on 30-Jun-11 11:31 AM
Hi,

You can forcefully close browser window using C#

Closing a window causes the Closing event to be raised. If the Closing event isn't canceled, the following occurs:

    * The Window is removed from Application.Windows (if an Application object exists).
    * The Window is removed from the owner Window if the owner/owned relationship was established before the owned Window was shown and after the owner Window was opened.
    * The Closed event is raised.
    * Unmanaged resources created by the Window are disposed.
    * If ShowDialog was called to show the Window, ShowDialog returns.

Closing a Window causes any windows that it owns to be closed. Furthermore, closing a Window may cause an application to stop running depending on how the Application.ShutdownMode property is set.

Following code should be used to closed the browser windows forcefully

void fileExitMenuItem_Click(object sender, RoutedEventArgs e)
{
    // Close this window
    this.Close();
}



void fileExitMenuItem_Click(object sender, RoutedEventArgs e)
{
    // Close this window
    this.Close();
}

void fileExitMenuItem_Click(object sender, RoutedEventArgs e)
{
    // Close this window
    this.Close();
}

Kirtan Patel replied to john sat on 30-Jun-11 10:50 PM
here is code to do it with javascript 

< a href="javascript:window.opener='x';window.close();">Close< /a>

Thanks