This post is kind of old... so I assume that Jeremy solved his problem several years ago... for others who are searching for a way to close a window on the server server side = as it has been said many times - this is a client (javascript) function. ANd an easy one at that. But if you want to avoid writing a litte javascript, heres your code-behind button to close a window.
protected void btnClose_Click(object sender, EventArgs e)
{
string jString = @"<script language=javascript>window.open('','_self',''); window.top.close();</script>";
ClientScriptManager cs = Page.ClientScript;
Type csType = this.GetType();
if (!cs.IsStartupScriptRegistered(jString))
{
cs.RegisterStartupScript(csType, "clientScript", jString);
}
}
I've kept the code a little verbose (making un-needed assignments to show more completely what is going on.
But wait - theres more. In my opion this is not really that different from using javascript function on page. But you can get a little re-usability (and provide a little abstraction as well, by putting this code in class that you call from button event or wherever. All you need is a reference to a page.
In other words...like one below, which I named jsCloseWindow(this.Page);
protected void btnClose_Click(object sender, EventArgs e)
{
dcPages.PageUtil.jsCloseWindow(this.Page);
}
Here's the code for jsCloseWindow
public static void jsCloseWindow(Page thisPage)
{
string jString = @"<script language=javascript>window.open('','_self',''); window.top.close();</script>";
ClientScriptManager cs = thisPage.ClientScript;
Type csType = thisPage.GetType();
if (!cs.IsStartupScriptRegistered(jString))
{
cs.RegisterStartupScript(csType, "clientScript", jString);
}
}
and the slimmed , down slightly less readable version:
public static void jsCloseWindow(Page thisPage)
{
string jString = @"<script language=javascript>window.open('','_self',''); window.top.close();</script>";
thisPage.ClientScript.RegisterStartupScript(thisPage.GetType(), "clientScript", jString);
if (!thisPage.ClientScript.IsStartupScriptRegistered(jString))
{
thisPage.ClientScript.RegisterStartupScript(thisPage.GetType(), "clientScript", jString);
}
}
Thanks to post from Hassaim on the window.open call before closing with window.top.close(). This prevents the message that "The webpage you are viewing is trying to close this window."
Hi Friends,
I simply want to print a report by opening a popup, calling the print
function and closing the window after the user has printed the
report.
I have 2 functions.... in c#
The Onload event registers a script to close the window from c#. The
FillReport functions fills a gridview
I have a JavaScript Function AssignContent(); which simply calls
window.print() and window.close()
This code runs fine in Firefox but the window is not closing in
Internet Explorer 6
I am also giving the code ... plz suggest me some solution.. i simply
cannot close the window in ie6.
Here is the code....
C# Code
---------------------------------------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fillreport();
if (!ClientScript.IsClientScriptBlockRegistered("k1"))
{
ClientScript.RegisterClientScriptBlock(this.GetType(),
"k1", "<script language='javascript'>AssignContent();</
script>");
}
}
}
private void fillreport()
{
//fills a gridview
}
//JavaScript
-------------------------------------
function AssignContent()
{
window.print();
window.close();
}
Regards,
Mahernoz
On Wednesday, November 21, 2007 7:06 PM Jeremy wrote:
Mahernoz, did you ever resolve this question? I have the same problem, and
I see that no one has replied to you.
Jeremy