tags:

views:

70

answers:

5

I have asp.net button "OK" in html popup window. I after my logic done how close that popup window it self?

<asp:Button Id="btnOK" runat="server"  AccessKey="<%$Resources:
wss,multipages_okbutton_accesskey%>" Width="70px" Text="<%$Resources:wss,
multipages_okbutton_text%>" OnClick="btnOK_Click" />
+6  A: 
<asp:Button ID="btnOK" runat="server" OnClientClick="window.close(); return false;" Text="Close" />
KBoek
A: 

That requires some javascript. Change your button markup to this:

<asp:Button Id="btnOK" runat="server"  AccessKey="<%$Resources:
wss,multipages_okbutton_accesskey%>" Width="70px" Text="<%$Resources:wss,
multipages_okbutton_text%>" OnClick="btnOK_Click" OnClientClick="javascript:window.close(); return false;" />
Ender
+1  A: 

If there is a chance that your server side code may fail, and you need to keep the popup open to correct errors, the OnClientClick trick won't help. I do this with a PlaceHolder and a small script:

<asp:PlaceHolder id="close_script" runat="server">
  <script>window.close();</script>
</asp:PlaceHolder>

Then, in the button handler, set the Visible property of the PlaceHolder to close the popup (or leave it open:

protected void btnOK_Click(Object sender, EventArgs e) {
  bool success = processPage();
  close_script.Visible = success;
}
Ray
If the `<asp:button>` itself renders, is it not relatively safe to assume that it would render the `OnClientClick` attribute? Honest question, but my guess is "you can safely assume that if the button even shows up that the proper javascript would be attached".
Daniel DiPaolo
true statement, but I don't see how it applies to my suggestion.
Ray
Ah it wasn't clear from the original version of your answer that the `PlaceHolder` would be on the parent page. For some reason I thought you were suggesting an additional control on the popup handling its own closure, which was just silly :)
Daniel DiPaolo
Actually, that is exactly what I am doing. The popup server-side code knows if it succeeded or failed, and therefore knows if it should close or stay open. Using the OnClientClick property (as others suggested) is almost the same thing, except that it closes the page *before* the server-side code runs. My suggestion closes the popup *after* the server-side code runs.
Ray
Why not just use registerstartupscript to write the js? Same lines of server-side code and much cleaner markup.
Fishcake
Personal preference mostly. However, if the acript is more complicated than 'window.close()', the RegisterStartupScript approach becomes uglier (IMO). I prefer to write my script in <script> blocks rather than by assembling strings in server side code.
Ray
+1  A: 

All correct but there is another way if you want close the window in your code:

Suppose that the button ID is "ContineButton" and the click event handler name is "ContineButton_Click"

protected void ContineButton_Click(object sender, EventArgs e)
{
    this.ClientScript.RegisterClientScriptBlock(this.GetType(), "Close", "window.close()", true);
}
amkh
A: 

There are other things to consider here - an accessible site will work without JavaScript including opening and closing a popup window. It will be dumbed down, but still function. Take a look at this article:

http://accessify.com/features/tutorials/the-perfect-popup/

NightOwl888