Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Close web form from code-behind

Reply
Thread Tools

Close web form from code-behind

 
 
John Straumann
Guest
Posts: n/a
 
      06-19-2009
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.


 
Reply With Quote
 
 
 
 
John Straumann
Guest
Posts: n/a
 
      06-19-2009
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]" <(E-Mail Removed)> wrote in message
news:#(E-Mail Removed)...
> "John Straumann" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> 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


 
Reply With Quote
 
 
 
 
John Straumann
Guest
Posts: n/a
 
      06-19-2009
Thanks, Mark!

John.

"Mark Rae [MVP]" <(E-Mail Removed)> wrote in message
news:#(E-Mail Removed)...
> "John Straumann" <(E-Mail Removed)> wrote in message
> news(E-Mail Removed)...
>
>> 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


 
Reply With Quote
 
Gregory A. Beamer
Guest
Posts: n/a
 
      06-19-2009
"John Straumann" <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> 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! |
*******************************************
 
Reply With Quote
 
Gregory A. Beamer
Guest
Posts: n/a
 
      06-19-2009
"Mark Rae [MVP]" <(E-Mail Removed)> wrote in
news:um6$(E-Mail Removed):

> "Gregory A. Beamer" <(E-Mail Removed)> 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! |
*******************************************
 
Reply With Quote
 
Gregory A. Beamer
Guest
Posts: n/a
 
      06-19-2009
"Mark Rae [MVP]" <(E-Mail Removed)> wrote in
news:#(E-Mail Removed):

> "Gregory A. Beamer" <(E-Mail Removed)> 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! |
*******************************************
 
Reply With Quote
 
Nitro Nitro is offline
Junior Member
Join Date: Oct 2012
Posts: 1
 
      10-02-2012
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...

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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to close a TCP socket? (TCPSocket#close doesn't close it) IƱaki Baz Castillo Ruby 7 01-12-2010 01:32 PM
Why does JdbcRowSetImpl.close() close the database connection? Paul van Rossem Java 0 04-07-2005 07:01 PM
close child form when parent form is closed. =?Utf-8?B?UGF1bA==?= ASP .Net 2 11-24-2004 10:31 AM
JavaMail POP3 folder.close() method close also store! Daniel Albisser Java 1 04-07-2004 03:45 PM
How to close child browser while parent close? Denon ASP .Net 1 11-14-2003 08:14 AM



Advertisments