How to close a Window from C# codebehind?

M

Mahernoz

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
 
J

Jeremy

Mahernoz, did you ever resolve this question? I have the same problem, and
I see that no one has replied to you.

Jeremy
 
M

Mark Rae [MVP]

I simply cannot close the window in IE6.

What happens when you try...?
ClientScript.RegisterClientScriptBlock(this.GetType(), "k1", "<script
language='javascript'>AssignContent();</> script>");

If you use the boolean overload, that will add the <script> tags
automatically so you avoid using the deprecated syntax...

ClientScript.RegisterClientScriptBlock(GetType(), "k1", "AssignContent();",
true);
function AssignContent()
{
window.print();
window.close();
}

Try adding window.opener=null; between the two statements above...
 
J

Jeremy

Mark, thanks for jumping in here.

I confess that I don't understand how Mahernoz was attempting to execute his
javascript. In my case, I simply did the following (after using your
suggestion about opener). Still does not work. Probably something very
basic I don't get.

protected void OnLoggedIn(object sender, EventArgs e)
{
Response.Write ("<script language =
javascript>window.opener=null; window.close();</script>");
}

Jeremy
 
M

Mark Rae [MVP]

I confess that I don't understand how Mahernoz was attempting to execute
his javascript. In my case, I simply did the following (after using your
suggestion about opener). Still does not work. Probably something very
basic I don't get.

Avoid using Response.Write to pump JavaScript into HTML streams in ASP.NET -
use ClientScript.RegisterClientScriptBlock instead.

And, use the boolean overload so that you avoid using deprecated syntax like
<script language=javascript>
 
J

Jeremy

Ok, I take your point that pumping java into HTML is a bad practice. Once I
figure out how to close a window from C#, I'll work on that.

But first, why does my pumped code fail? Even if I have a javascript
function, I have no idea how to call it from the codebehind.

Jeremy
 
M

Mark Rae [MVP]

Ok, I take your point that pumping java into HTML is a bad practice.

I think you mean JavaScript, not Java... :)
Even if I have a JavaScript function, I have no idea how to call it from
the codebehind.

<head>
<script type="text/javascript">
function showMessage(pstrMessage)
{
alert(pstrMessage);
if (confirm('Would you like to close the window?'))
{
window.opener=null;
window.close();
}
}
</script>
</head>

<asp:Button ID="MyButton" runat="server" Text="Close"
OnClick="MyButton_Click" />

protected void MyButton_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(GetType(), "close",
"showMessage('Hello');", true);
}
 
J

Jeremy

Mark, forgive me, but you really haven't read my question. I know how to
put a button on a form & use event handlers.

Anyway, I have an answer from someone else. It involves response.end(),
following the response.write(). If you really have a better answer, I'd be
glad to hear it.
 
M

Mark Rae [MVP]

Mark, forgive me, but you really haven't read my question. I know how to
put a button on a form & use event handlers.

My apologies - when you said "Even if I have a JavaScript function, I have
no idea how to call it from the codebehind.", I thought that you meant that
you wanted to know how to call a JavaScript function from codehind.
 
J

Juan T. Llibre

re:
!> It involves response.end(), following the response.write().

Why would you want to use response.write and response.end in ASP.NET ?
In ASP, they're fine but in ASP.NET response.write writes outside the body.

re:
!> If you really have a better answer

He already gave it to you :

!> Avoid using Response.Write to pump JavaScript into HTML streams in
!> ASP.NET - use ClientScript.RegisterClientScriptBlock instead.
!> avoid using deprecated syntax like <script language=javascript>

Try this test :

<%@ Page Language="VB" EnableViewState="false" strict="false"%>
<script runat="server">
sub Page_Load(obj as object, e as eventargs)
Response.Write("test")
End Sub
</script>
<html>
<body>
</body>
</html>

....and "View Source" to see where ASP.NET writes the word "test".

You may be surprised to see it outside of the <html> tags.





Jeremy said:
Mark, forgive me, but you really haven't read my question. I know how to put a button on a form & use event
handlers.

Anyway, I have an answer from someone else. It involves response.end(), following the response.write(). If you
really have a better answer, I'd be glad to hear it.
 
J

Jeremy

Ok, I stipulate that I was not clear in my question. Here it is.

I am using a login control on a popup, and in the OnLoggedIn event handler I
want to close the popup and return to the calling window. From this event
handler, I do not know how to close the window, other than to pump
javascript into the html. The following does work, now that I've added the
2nd line of code.

protected void OnLoggedIn(object sender, EventArgs e)
{
Response.Write("<script language=javascript> window.close();
</script>");
Response.End();
}

You may have guessed that I'm an asp beginner and am learning it the hard
way.

Jeremy

Juan T. Llibre said:
re:
!> It involves response.end(), following the response.write().

Why would you want to use response.write and response.end in ASP.NET ?
In ASP, they're fine but in ASP.NET response.write writes outside the
body.

re:
!> If you really have a better answer

He already gave it to you :

!> Avoid using Response.Write to pump JavaScript into HTML streams in
!> ASP.NET - use ClientScript.RegisterClientScriptBlock instead.
!> avoid using deprecated syntax like <script language=javascript>

Try this test :

<%@ Page Language="VB" EnableViewState="false" strict="false"%>
<script runat="server">
sub Page_Load(obj as object, e as eventargs)
Response.Write("test")
End Sub
</script>
<html>
<body>
</body>
</html>

...and "View Source" to see where ASP.NET writes the word "test".

You may be surprised to see it outside of the <html> tags.
 
J

Jeremy

One more thing: I think I understand that registering a clientscriptblock
can create javascript. However, I have no idea how to execute this from my
OnLoggedIn event handler. Adding another button to the form (which already
has a Log In button) defeats the purpose, and closing in the click event for
the log in button also fails by always closing the window, even when the
user is not authenticated.

Jeremy

Juan T. Llibre said:
re:
!> It involves response.end(), following the response.write().

Why would you want to use response.write and response.end in ASP.NET ?
In ASP, they're fine but in ASP.NET response.write writes outside the
body.

re:
!> If you really have a better answer

He already gave it to you :

!> Avoid using Response.Write to pump JavaScript into HTML streams in
!> ASP.NET - use ClientScript.RegisterClientScriptBlock instead.
!> avoid using deprecated syntax like <script language=javascript>

Try this test :

<%@ Page Language="VB" EnableViewState="false" strict="false"%>
<script runat="server">
sub Page_Load(obj as object, e as eventargs)
Response.Write("test")
End Sub
</script>
<html>
<body>
</body>
</html>

...and "View Source" to see where ASP.NET writes the word "test".

You may be surprised to see it outside of the <html> tags.
 
B

Barrie Wilson

Jeremy said:
Ok, I stipulate that I was not clear in my question. Here it is.

I am using a login control on a popup, and in the OnLoggedIn event handler
I want to close the popup and return to the calling window. From this
event handler, I do not know how to close the window, other than to pump
javascript into the html. The following does work, now that I've added
the 2nd line of code.

protected void OnLoggedIn(object sender, EventArgs e)
{
Response.Write("<script language=javascript> window.close();
</script>");
Response.End();
}

You may have guessed that I'm an asp beginner and am learning it the hard
way.

maybe I'm missing something but it seems it would have to be client-side
code; there may be other, maybe better(?) ways to get the javascript code
in there but if this works, it works, no?
 
B

Barrie Wilson

One more thing: I think I understand that registering a clientscriptblock
can create javascript. However, I have no idea how to execute this from
my OnLoggedIn event handler. Adding another button to the form (which
already has a Log In button) defeats the purpose, and closing in the click
event for the log in button also fails by always closing the window, even
when the user is not authenticated.

this feels like a bit of a kludge, but a simple one, and it appears to work:

// add to your ASPX code
<head runat="server">
<title>Login Page</title>

<script language=javascript>
var Loggedin = <% =ssLoggedin %>

function ckLogged()
{
if (Loggedin == "1")
window.close();
}
</script>
</head>

<body onload="javascript:ckLogged()">

// in your C# Code-Behind
public partial class myLoginClass : System.Web.UI.Page
{
public string ssLoggedin = "0";

....

protected void OnLoggedIn(object sender, EventArgs e)
{
ssLoggedin = "1";
}
}
 
J

Jeremy

Barrie, thanks. I read about code blocks, then tried this out. Good stuff
to know. However, the asp login control wants to display the calling window
after login, so the onload event never fires.

Anyway, it seems to me my architecture needs changing. I'm going to do away
with the popup, and have the login page load at startup, then load the main
page in the same window on successfully authentication.
 
B

Barrie Wilson

Barrie, thanks. I read about code blocks, then tried this out. Good stuff
to know. However, the asp login control wants to display the calling
window after login, so the onload event never fires.

I could be wrong on this but I thought the login control let you set the
post-login behavior .. a go-to URL property ... ??
Anyway, it seems to me my architecture needs changing. I'm going to do
away with the popup, and have the login page load at startup, then load
the main page in the same window on successfully authentication.

won't argue with you here; I'm not much of a fan of popups anyway
 
J

Jeremy

Barrie Wilson said:
I could be wrong on this but I thought the login control let you set the
post-login behavior .. a go-to URL property ... ??

There is a property for an image url. I'm searching now for where to put
the page url. I think it's hidden in the web.config for some reason.
 

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

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top