Home All Groups Group Topic Archive Search About

Close web form from code-behind

Author
19 Jun 2009 1:04 PM
John Straumann
Hi all:

I have a button on a web form that calls a method in the code-behind file,

OnClick="cmdButton_Click"

and I am wondering if it is possible to close the web form from the
code-behind? Or would I have to use client-side script to call the
code-behind method, and then close the form from the client?

Can anyone make a suggestion and/or point me at an info source?

Thanks all!

John.

Author
19 Jun 2009 1:19 PM
Mark Rae [MVP]
"John Straumann" <jstraum***@hotmail.com> wrote in message
news:6B4853E2-2EA9-483C-B0B5-DB69CF051BFF@microsoft.com...

> I am wondering if it is possible to close the web form from the
> code-behind?

It isn't possible to close a browser window from code-behind because
code-behind runs on the webserver, not in the client browser...

> Can anyone make a suggestion and/or point me at an info source?

You can, of course, use code-behind to generate client-side JavaScript which
will be streamed to the client along with the rest of the HTML stream.

However, unless you need to perform server-side processing, there is no need
to use a server-side method for this e.g.

<asp:Button ID="cmdClose" runat="server" OnClientClick="window.close();"
Text="Close" />

However, this will (almost) always pop up the "Something is trying to close
this window" confirmation message. It used to be possible to prevent this in
earlier versions of IE (and some other browsers), but this has thankfully
now been fixed in most later versions...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Are all your drivers up to date? click for free checkup

Author
19 Jun 2009 1:31 PM
John Straumann
Hi mark:

Thank you for your reply. Yes I do need to perform server-side processing,
so am I out of luck to close the form?

John.


Show quoteHide quote
"Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote in message
news:#fgTLCO8JHA.1492@TK2MSFTNGP03.phx.gbl...
> "John Straumann" <jstraum***@hotmail.com> wrote in message
> news:6B4853E2-2EA9-483C-B0B5-DB69CF051BFF@microsoft.com...
>
>> I am wondering if it is possible to close the web form from the
>> code-behind?
>
> It isn't possible to close a browser window from code-behind because
> code-behind runs on the webserver, not in the client browser...
>
>> Can anyone make a suggestion and/or point me at an info source?
>
> You can, of course, use code-behind to generate client-side JavaScript
> which will be streamed to the client along with the rest of the HTML
> stream.
>
> However, unless you need to perform server-side processing, there is no
> need to use a server-side method for this e.g.
>
> <asp:Button ID="cmdClose" runat="server" OnClientClick="window.close();"
> Text="Close" />
>
> However, this will (almost) always pop up the "Something is trying to
> close this window" confirmation message. It used to be possible to prevent
> this in earlier versions of IE (and some other browsers), but this has
> thankfully now been fixed in most later versions...
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net
Author
19 Jun 2009 2:00 PM
Mark Rae [MVP]
"John Straumann" <jstraum***@hotmail.com> wrote in message
news:D7DD1487-3C00-4E59-9791-43B0CFF6FC54@microsoft.com...

> Thank you for your reply. Yes I do need to perform server-side processing,
> so am I out of luck to close the form?

<asp:Button ID="cmdClose" runat="server" OnClick="cmdClose_Click"
OnClientClick="return confirm('Are you sure?');" Text="Close" />


protected void cmdClose_Click(object sender, EventArgs e)
{
    // server-side processing...

    ClientScript.RegisterStartupScript(GetType(), "close",
"window.close();", true);
}


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Author
19 Jun 2009 2:11 PM
John Straumann
Thanks, Mark!

John.

Show quoteHide quote
"Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote in message
news:#xtWMZO8JHA.1740@TK2MSFTNGP02.phx.gbl...
> "John Straumann" <jstraum***@hotmail.com> wrote in message
> news:D7DD1487-3C00-4E59-9791-43B0CFF6FC54@microsoft.com...
>
>> Thank you for your reply. Yes I do need to perform server-side
>> processing, so am I out of luck to close the form?
>
> <asp:Button ID="cmdClose" runat="server" OnClick="cmdClose_Click"
> OnClientClick="return confirm('Are you sure?');" Text="Close" />
>
>
> protected void cmdClose_Click(object sender, EventArgs e)
> {
>    // server-side processing...
>
>    ClientScript.RegisterStartupScript(GetType(), "close",
> "window.close();", true);
> }
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net
Author
19 Jun 2009 2:49 PM
Gregory A. Beamer
Show quote Hide quote
"John Straumann" <jstraum***@hotmail.com> wrote in
news:6B4853E2-2EA9-483C-B0B5-DB69CF051BFF@microsoft.com:

> Hi all:
>
> I have a button on a web form that calls a method in the code-behind
> file,
>
> OnClick="cmdButton_Click"
>
> and I am wondering if it is possible to close the web form from the
> code-behind? Or would I have to use client-side script to call the
> code-behind method, and then close the form from the client?
>
> Can anyone make a suggestion and/or point me at an info source?


You cannot close directly from CodeBehind, but you can do something like
this (watch word wrap):

string close;
close = "<script language=\"javascript\">windows.Close();</script>";

LiteralControl lit = new LiteralControl(close);

ContainerControl.Controls.Add(lit);

NOTE: ContainerControl here is something like a panel or other container
that you can anchor code into.


An even better option is outputting client script, but I felt I was more
likely to have to test the code if I did that. The end result is you are
injecting javaScript to close the page.



--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
Show quoteHide quote
|      Think outside the box!             |
*******************************************
Author
19 Jun 2009 3:17 PM
Mark Rae [MVP]
"Gregory A. Beamer" <NoSpamMgbworld@comcast.netNoSpamM> wrote in message
news:Xns9C2F63F0CCE52gbworld@207.46.248.16...

> close = "<script language=\"javascript\">windows.Close();</script>";

The language attribute of the <script /> tag has been deprecated for almost
13 years.


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Author
19 Jun 2009 5:12 PM
Gregory A. Beamer
"Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote in
news:um6$IEP8JHA.5704@TK2MSFTNGP03.phx.gbl:

> "Gregory A. Beamer" <NoSpamMgbworld@comcast.netNoSpamM> wrote in
> message news:Xns9C2F63F0CCE52gbworld@207.46.248.16...
>
>> close = "<script language=\"javascript\">windows.Close();</script>";
>
> The language attribute of the <script /> tag has been deprecated for
> almost 13 years.


I guess I am showing my age now, am I? ;-)

Note to self: type="text/javascript" is probably better here. I generally
do not have to type these things out any more due to Intellisense. Damn you
Intellisense.


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
Show quoteHide quote
|      Think outside the box!             |
*******************************************
Author
19 Jun 2009 5:48 PM
Mark Rae [MVP]
"Gregory A. Beamer" <NoSpamMgbworld@comcast.netNoSpamM> wrote in message
news:Xns9C2F7C45EE4F3gbworld@207.46.248.16...

> Note to self: type="text/javascript" is probably better here.

Yes but, as you correctly hinted, outputting client script (via the
ClientScript namespace) is an even better option since, with the boolean
override, you don't need to worry about the script tags at all...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Author
19 Jun 2009 8:35 PM
Gregory A. Beamer
Show quote Hide quote
"Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote in
news:#TiXkYQ8JHA.4820@TK2MSFTNGP04.phx.gbl:

> "Gregory A. Beamer" <NoSpamMgbworld@comcast.netNoSpamM> wrote in
> message news:Xns9C2F7C45EE4F3gbworld@207.46.248.16...
>
>> Note to self: type="text/javascript" is probably better here.
>
> Yes but, as you correctly hinted, outputting client script (via the
> ClientScript namespace) is an even better option since, with the
> boolean override, you don't need to worry about the script tags at
> all...
>
>

And, if I weren't lazy this morning, I would have coded the client
script output to ensure syntax. ;-)

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
Show quoteHide quote
|      Think outside the box!             |
*******************************************

Bookmark and Share