Visual Studio .NET - How to close the form? - Asked By Kannabiran . on 08-Mar-07 10:10 AM

 Im using c#,im trying to close the webform on button click,is it possible to achieve it?if yes,explain it.Here i dont want use any javascripts to close the form and while closing the window i dont need any confirmation message to close the window.I need this by a server side code.could anybody knows help me...Please...

In windows me.close()
but in web???


Can't do it server side... - Sean Patterson replied to Kannabiran . on 08-Mar-07 11:31 AM

You need to have some kind of Javascript based close command in order for the webform to close. Remember, in ASP.NET based apps, all the "processing" is done on the serverside, where it has no direct control over the client computer, it can only send DHTML/CSS/XML/Javascript back to the client in order to process what the user interacts wtih.

The one advantage you do have is that you can wait until the user clicks "Close" button and then register a javascript client block to issue a close command so that there isn't a miscellaneous "close" javascript function floating around your app the whole time, but that's the closest you'll come.

This is the classic misunderstanding - Peter Bromberg replied to Kannabiran . on 08-Mar-07 01:01 PM

between what represents "server side" and what is "client side".

This will "close" a browser window:

<input type="button" onclick="javascript:window.close();" value="close me" />

You can also use the OnClientClick property - mv ark replied to Kannabiran . on 09-Mar-07 12:16 AM

 to execute the Javascript command window.close(), if you are using a Button server control .OnClientClick property is a new property in ASP.NET 2.0.

You are probably concerned about the alert - "The web page you are viewing is trying to close the window." that needs user confirmation when you try to close the parent page.

The fact is that owing to security reasons, you cannot close windows you don't open yourself and this is the documented behaviour of window.close()

"The close method closes only windows opened by JavaScript using the open method. If you attempt to close any other window, a confirm is generated, which lets the user choose whether the window closes. This is a security feature to prevent "mail bombs" containing self.close(). However, if the window has only one document (the current one) in its session history, the close is allowed without any confirm. This is a special case for one-off windows that need to open other windows and then dispose of themselves." form close - P c replied to Kannabiran . on 10-Sep-07 02:48 PM

hi u can use onle line code:

btnClose.Attributes.Add(onclick ,"window.close()");


Pawan Chand



Foyzul Karim replied to P c on 05-Oct-10 05:48 AM
I think the syntax would be
 btnClose.Attributes.Add("onclick" ,"window.close()");