Close web form from code-behind

Discussion in 'ASP .Net' started by John Straumann, Jun 19, 2009.

  1. 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, Jun 19, 2009
    #1
    1. Advertising

  2. 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]" <> wrote in message
    news:#...
    > "John Straumann" <> wrote in message
    > news:...
    >
    >> 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, Jun 19, 2009
    #2
    1. Advertising

  3. Thanks, Mark!

    John.

    "Mark Rae [MVP]" <> wrote in message
    news:#...
    > "John Straumann" <> wrote in message
    > news:D...
    >
    >> 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
     
    John Straumann, Jun 19, 2009
    #3
  4. "John Straumann" <> wrote in
    news::

    > 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, Jun 19, 2009
    #4
  5. "Mark Rae [MVP]" <> wrote in
    news:um6$:

    > "Gregory A. Beamer" <> 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, Jun 19, 2009
    #5
  6. "Mark Rae [MVP]" <> wrote in
    news:#:

    > "Gregory A. Beamer" <> 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! |
    *******************************************
     
    Gregory A. Beamer, Jun 19, 2009
    #6
  7. John Straumann

    Nitro

    Joined:
    Oct 2, 2012
    Messages:
    1
    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
     
    Nitro, Oct 2, 2012
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    594
    Alphonse Giambrone
    Jun 28, 2003
  2. Daniel Manes

    Code-Behind Pain in the Behind!

    Daniel Manes, Jun 10, 2005, in forum: ASP .Net
    Replies:
    11
    Views:
    669
  3. John

    Non-code behind to code behind

    John, Feb 19, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    491
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 19, 2007
  4. Kumarforg

    Web Service invocation from behind proxy behind firewall

    Kumarforg, Aug 3, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    193
    Kumarforg
    Aug 3, 2004
  5. Iñaki Baz Castillo
    Replies:
    7
    Views:
    874
    Iñaki Baz Castillo
    Jan 12, 2010
Loading...

Share This Page