Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a .aspx page in which I have a Asp:Button, clicking on which user browser(Opera) tab will be closed. After googling alot a found a solution to acheive this from code behind. (for Opera)
XML
<asp:Button ID="BTN" runat="server" Text="close the window" onclick="BTN_Click"/>

I have put this code in Page.Load event.

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

This is What it renders in HTML

XML
<input type="submit" name="BTN" value="close the window" onclick="window.close();" id="BTN">


It is working fine as expected, user clicks the button and window is closed. Please note down browser is Opera.

Now look at the same code, the only difference is the button is inheriting Master Page and Content Panel Properties, so in this case it is rendered like this

XML
<input type="submit" name="ctl00$MainContentPlaceHolder$BTN" value="close the window by clicking me" onclick="window.close();" id="ctl00_MainContentPlaceHolder_BTN" clientidmode="static">


Now user clicks on this but unexpectedly it just refreshes the browser window and doesn't close the window.
I have put these rendered codes here because i firmly beleive it is the ID which is the culprit. If it is so Can you please suggest me how to achieve this from code behind??
Posted
Updated 16-Jun-15 22:13pm
v2
Comments
Sreekanth Mothukuru 17-Jun-15 1:55am    
First, try to find the button control with the name and then apply "onclick" attributes to it. Whatever be the id at runtime, you can find it using the id.
RVPathi 17-Jun-15 2:39am    
Yep, thats what i Want. But how?
till now I have tried this:
MasterPage ctl00 = FindControl("ctl00") as MasterPage;
ContentPlaceHolder MainContent = ctl00.FindControl("MainContentPlaceHolder") as ContentPlaceHolder;
Button BTN1 = MainContent.FindControl("BTN") as Button;

what next??
Sreekanth Mothukuru 17-Jun-15 2:45am    
You just don't need to find the master page. Only try to find the button control on the page. Like,
this.FindControl("Btn") or Page.FindControl("Btn")
RVPathi 17-Jun-15 2:52am    
will that give the rendered ID of control??
Sreekanth Mothukuru 17-Jun-15 2:58am    
The statement will give you button control if it exists on the page. You can use "Page.FindControl("Btn").Id" to get the button runtime Id something similar to "ctl00_MainContentPlaceHolder_BTN"

1 solution

You can only close a tab/window which you create. You cannot close a window/tab programmatically.

eg.: You can create a window using window.open() then only you can close that window using window.close().

--Amy
 
Share this answer
 
Comments
RVPathi 17-Jun-15 2:35am    
Window.open() or window.close() works fine from UI, but not from code behind. therefore i have tried BTN.Attributes.Add("onclick", "window.close();");. As I said it is working as expected from normal page, but when it comes to inheriting masterpage and content panels, page just refreshes rather than closing itself. Solution must be hidden somewhere if I am able achieve rendered ID of button contol from code behind. as there is some code in code behind which has to be executed first so I cant impose client side scripting in this scenario.hope u have got my point now.
_Amy 17-Jun-15 2:48am    
You did not understand my answer I think. You can't close the current window in any browser because you didn't open it. You should check your browser console and see the error.
And if it works in Chrome and other browsers, that's a serious bug and you should report it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900