[c#] Programmatically close aspx page from code behind

For anyone wondering why they cannot get the provided answers to work it's because the page must have been opened by javascript in order to be closed by javascript.

Since most people finding this asp question are likely using an asp:hyperlink or an asp redirect of some sort to navigate to the page that needs to be closed. These methods of redirection don't use javascript and therefore will not close by javascript.

I found a simple solution for my application and that's eliminating the NavigateUrl and using the asp:Hyperlink.Attributes to add an onclick to the hyperlink which uses java script to open the window that needs to be closed by javascript.

aspHyperlink.NavigateUrl = "https://www.google.com";

The above NavigateUrl is removed and instead we attach our click event.

aspHyperlink.Attributes.Add("onclick", "javascript:openInNewTab('https://www.google.com');");

And in the code behind of the page containing our aspHyperlink we have the javascript for opening the url provided by Rinto

function openInNewTab(url) {
         var win = window.open(url, '_blank');
         win.focus();
     } 

Now all pages opened with openInNewTab can be closed with the provided answers.

window.close();