How to close a Window from C# codebehind?

S

Scott Roberts

Jeremy,

I think you got confused because he gave you two examples in one post. All
you need is "ClientScript.RegisterStartupScript". This not only "creates
javascript in HTML" but causes it to execute when the page loads. So, if you
call "ClientScript.RegisterStartupScript" from within your server-side
event, the javascript will execute when the page loads (i.e. in the browser,
after the postback).

Try it with a sample page. Just put a regular button with a server-side
event and in the server-side event call:

ClientScript.RegisterStartupScript(GetType(), "close",
"showMessage('Hello');", true);

You'll notice that you get a message box on the client after you click the
button. Substitute "showMessage('Hello')" for "window.close()" and see what
happens too.

Scott
 
J

Jeremy

For whatever reason, I was and am continually confused! I see now that the
example uses RegisterStartupScript, and obviously the word Startup is a big
clue. The first example from Mahernoz used RegisterClientScript, and I
simply spaced out on the change when it appeared later. By the way, a good
argument in favor of Phonics vs Whole Word recognition. But anyway.

Thanks

Jeremy
 
D

Dave Conner

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top