|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Close web form from code-behindHi 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" <jstraum***@hotmail.com> wrote in message It isn't possible to close a browser window from code-behind because news:6B4853E2-2EA9-483C-B0B5-DB69CF051BFF@microsoft.com... > I am wondering if it is possible to close the web form from the > code-behind? 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... 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 "John Straumann" <jstraum***@hotmail.com> wrote in message <asp:Button ID="cmdClose" runat="server" OnClick="cmdClose_Click"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? 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); } 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
Show quote
Hide quote
"John Straumann" <jstraum***@hotmail.com> wrote in You cannot close directly from CodeBehind, but you can do something like 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? 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. -- Show quoteHide quoteGregory A. Beamer MVP; MCP: +I, SE, SD, DBA Twitter: @gbworld Blog: http://gregorybeamer.spaces.live.com ******************************************* | Think outside the box! | ******************************************* "Gregory A. Beamer" <NoSpamMgbworld@comcast.netNoSpamM> wrote in message The language attribute of the <script /> tag has been deprecated for almost news:Xns9C2F63F0CCE52gbworld@207.46.248.16... > close = "<script language=\"javascript\">windows.Close();</script>"; 13 years. "Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote in I guess I am showing my age now, am I? ;-)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. 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. -- Show quoteHide quoteGregory A. Beamer MVP; MCP: +I, SE, SD, DBA Twitter: @gbworld Blog: http://gregorybeamer.spaces.live.com ******************************************* | Think outside the box! | ******************************************* "Gregory A. Beamer" <NoSpamMgbworld@comcast.netNoSpamM> wrote in message Yes but, as you correctly hinted, outputting client script (via the news:Xns9C2F7C45EE4F3gbworld@207.46.248.16... > Note to self: type="text/javascript" is probably better here. ClientScript namespace) is an even better option since, with the boolean override, you don't need to worry about the script tags at all...
Show quote
Hide quote
"Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote in And, if I weren't lazy this morning, I would have coded the clientnews:#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... > > script output to ensure syntax. ;-) -- Show quoteHide quoteGregory A. Beamer MVP; MCP: +I, SE, SD, DBA Twitter: @gbworld Blog: http://gregorybeamer.spaces.live.com ******************************************* | Think outside the box! | *******************************************
Other interesting topics
|
|||||||||||||||||||||||