简体   繁体   中英

How close Html window when click asp.net button?

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" />
<asp:Button ID="btnOK" runat="server" OnClientClick="window.close(); return false;" Text="Close" />

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

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

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;" />

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM