Close web form from code-behind

  • Thread starter Thread starter John Straumann
  • Start date Start date
J

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.
 
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.
 
Thanks, Mark!

John.

Mark Rae said:
<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);
}
 
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! |
*******************************************
 
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! |
*******************************************
 
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! |
*******************************************
 
Back
Top