Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   Close web form from code-behind (http://www.velocityreviews.com/forums/t688420-close-web-form-from-code-behind.html)

John Straumann 06-19-2009 01:04 PM

Close web form from code-behind
 
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.



John Straumann 06-19-2009 01:31 PM

Re: Close web form from code-behind
 
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.


"Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote in message
news:#fgTLCO8JHA.1492@TK2MSFTNGP03.phx.gbl...
> "John Straumann" <jstraumann@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



John Straumann 06-19-2009 02:11 PM

Re: Close web form from code-behind
 
Thanks, Mark!

John.

"Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote in message
news:#xtWMZO8JHA.1740@TK2MSFTNGP02.phx.gbl...
> "John Straumann" <jstraumann@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



Gregory A. Beamer 06-19-2009 02:49 PM

Re: Close web form from code-behind
 
"John Straumann" <jstraumann@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

*******************************************
| Think outside the box! |
*******************************************

Gregory A. Beamer 06-19-2009 05:12 PM

Re: Close web form from code-behind
 
"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

*******************************************
| Think outside the box! |
*******************************************

Gregory A. Beamer 06-19-2009 08:35 PM

Re: Close web form from code-behind
 
"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

*******************************************
| Think outside the box! |
*******************************************

Nitro 10-02-2012 03:39 PM

Close web-form from code-behind page...
 
I know this is a fairly old thread, but incase someone searches for this now, or in the future, I just wanted to add my $.02, so spend it wisely... I just tested this, and it worked exactly as desired... Happy coding...

And I'm a VB programmer (as if you couldn't tell by my post :) ), so modify code accordingly, and just as Gregory A. Beamer stated, if "I" wasn't lazy this morning, I'd add the C# syntax as well, but, I'm feeling somewhat lazy... :sleep:

Note: Watch for wrapped text here, I did not wrap the code, but this editor might have.

PrivateSub cmdSaveAndClose_Click(sender AsObject, e As System.EventArgs) Handles cmdSaveAndClose.Click
' Server-side processing...
'...
'...
'...
Dim jScript AsString = "this.focus();self.opener = this;self.close();"
Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "closeIt", jScript, True)
EndSub


All times are GMT. The time now is 07:13 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.