473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to close a Window from C# codebehind?

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.GetTyp e(),
"k1", "<script language='javascript'>AssignContent();</
script>");
}
}
}

private void fillreport()
{
//fills a gridview
}

//JavaScript
-------------------------------------
function AssignContent()
{
window.print();
window.close();
}


Regards,
Mahernoz

Sep 6 '07 #1
15 20248
Mahernoz, did you ever resolve this question? I have the same problem, and
I see that no one has replied to you.

Jeremy

"Mahernoz" <ma******@gmail.comwrote in message
news:11*********************@w3g2000hsg.googlegrou ps.com...
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.GetTyp e(),
"k1", "<script language='javascript'>AssignContent();</
script>");
}
}
}

private void fillreport()
{
//fills a gridview
}

//JavaScript
-------------------------------------
function AssignContent()
{
window.print();
window.close();
}


Regards,
Mahernoz
Nov 22 '07 #2
"Mahernoz" <ma******@gmail.comwrote in message
news:11*********************@w3g2000hsg.googlegrou ps.com...
I simply cannot close the window in IE6.
What happens when you try...?
ClientScript.RegisterClientScriptBlock(this.GetTyp e(), "k1", "<script
language='javascript'>AssignContent();</script>");
If you use the boolean overload, that will add the <scripttags
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...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 22 '07 #3
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

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:eC**************@TK2MSFTNGP05.phx.gbl...
"Mahernoz" <ma******@gmail.comwrote in message
news:11*********************@w3g2000hsg.googlegrou ps.com...
>I simply cannot close the window in IE6.

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

If you use the boolean overload, that will add the <scripttags
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...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Nov 22 '07 #4
"Jeremy" <je***********@ninprodata.comwrote in message
news:uX**************@TK2MSFTNGP05.phx.gbl...
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>
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 22 '07 #5
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

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
"Jeremy" <je***********@ninprodata.comwrote in message
news:uX**************@TK2MSFTNGP05.phx.gbl...
>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>
--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Nov 22 '07 #6
"Jeremy" <je***********@ninprodata.comwrote in message
news:e1**************@TK2MSFTNGP05.phx.gbl...
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);
}
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 22 '07 #7
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.

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:Oo**************@TK2MSFTNGP04.phx.gbl...
"Jeremy" <je***********@ninprodata.comwrote in message
news:e1**************@TK2MSFTNGP05.phx.gbl...
>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);
}
--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Nov 22 '07 #8
"Jeremy" <je***********@ninprodata.comwrote in message
news:Ox***************@TK2MSFTNGP04.phx.gbl...
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.
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 22 '07 #9
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 <htmltags.


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
"Jeremy" <je***********@ninprodata.comwrote in message news:Ox***************@TK2MSFTNGP04.phx.gbl...
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.

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message news:Oo**************@TK2MSFTNGP04.phx.gbl...
>"Jeremy" <je***********@ninprodata.comwrote in message news:e1**************@TK2MSFTNGP05.phx.gbl...
>>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);
}
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 22 '07 #10
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=javascriptwindow.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" <no***********@nowhere.comwrote in message
news:ee*************@TK2MSFTNGP03.phx.gbl...
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 <htmltags.


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
"Jeremy" <je***********@ninprodata.comwrote in message
news:Ox***************@TK2MSFTNGP04.phx.gbl...
>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.

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:Oo**************@TK2MSFTNGP04.phx.gbl...
>>"Jeremy" <je***********@ninprodata.comwrote in message
news:e1**************@TK2MSFTNGP05.phx.gbl...

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);
}
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 22 '07 #11
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" <no***********@nowhere.comwrote in message
news:ee*************@TK2MSFTNGP03.phx.gbl...
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 <htmltags.


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
"Jeremy" <je***********@ninprodata.comwrote in message
news:Ox***************@TK2MSFTNGP04.phx.gbl...
>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.

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:Oo**************@TK2MSFTNGP04.phx.gbl...
>>"Jeremy" <je***********@ninprodata.comwrote in message
news:e1**************@TK2MSFTNGP05.phx.gbl...

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);
}
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 22 '07 #12

"Jeremy" <je***********@ninprodata.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
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=javascriptwindow.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?

Nov 22 '07 #13

"Jeremy" <je***********@ninprodata.comwrote in message
news:ep**************@TK2MSFTNGP04.phx.gbl...
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
Nov 24 '07 #14
"Barrie Wilson" <bw*****@nowhere.comwrote in message
news:13*************@corp.supernews.com...
>
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.

Nov 24 '07 #15
Never mind. Yes, there is a destination url property. My mistake.
"Jeremy" <je***********@ninprodata.comwrote in message
news:O5****************@TK2MSFTNGP02.phx.gbl...
"Barrie Wilson" <bw*****@nowhere.comwrote in message
news:13*************@corp.supernews.com...
>>
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.
Nov 24 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Simon Fletcher | last post by:
Hello there, I need a "Close Window" script ( onLoad.Close.Window() ), however i need the script so it don't come up with this annoying window: "This window is trying to close" window. Is...
1
by: Ivan Sutton | last post by:
Hi, I'm quite new to Java: I installed a script which opened a window in a predefined format. It works fine! I want to know if it is possible to close that screen with java. (however, the...
1
by: Mark Rigsbee | last post by:
I was wonder if anyone out there could shed some light on a problem I am having. I'm having trouble getting a javascript "window.close()" method to work when running over a shared SSL connection....
6
by: chon | last post by:
I have an ASP page that is sent a file location as a parameter. It opens this file, loads the ADODB.Stream object and does a binary write to the page forcing the download dialog to appear. This...
2
by: BWGames | last post by:
Hi, I'm looking for a way to have a message box pop up as a page is loaded, then, when the user clicks OK on the message box, for the window to close. It will be a popup window, so there...
6
by: clequieu | last post by:
I have created a form. Within the form is a button to close the window on click and to validate as well. The close window works when it is a stand alone, but it does not work when it is embedded...
3
by: mcyi2mr3 | last post by:
Hi all I'm new to javascript and im trying to add a close button function to my certain pages of my site. I use this code: <a href='javascript:window.close();'>Close Window</a> to create a...
1
by: Kasya | last post by:
How Can I close Window without confirmation: --------------------------- Microsoft Internet Explorer --------------------------- The Web page you are viewing is trying to close the window. ...
4
by: Ubi | last post by:
I have a popup and I would like to capture the close window event. How can I do that in javascript? Daniele
2
by: holmes86 | last post by:
hi,everyone I have a GTK window,when click "close window" ,I want to show a verify dialog that whether verify really quit or not.so I used a MesssageDialog for verify quit.But when I clicked the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.