Close web form from code-behind

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.
 
J

John Straumann

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.
 
J

John Straumann

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);
}
 
G

Gregory A. Beamer

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.
 
G

Gregory A. Beamer

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.
 
G

Gregory A. Beamer

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. ;-)
 
Joined
Oct 2, 2012
Messages
1
Reaction score
0
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top