You must Sign In to post a response.
  • Category: ASP.NET

    How to close page using a button

    i want to close the webpage when i click the cancel button
  • #767272
    In Asp.Net

    Code Behind :

    protected void Page_Load(object sender, EventArgs e)
    Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "closePage", "window.onunload = CloseWindow();");

    In Aspx Page

    function CloseWindow() {

    Or add Attribute to the button to close the current window

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

    You can also use java script to do that

  • #767275

    you can try this code

    <html xmlns="">
    <head runat="server">
    <script language="javascript" type="text/javascript">
    function closeWindow()
    <form id="form1" runat="server">
    <asp:Button ID="BtnSave" runat="server" OnClientClick="return closeWindow();" Text="Save" />
    <asp:Button ID="Button1" runat="server" OnClientClick="javascript:window.close();" Text="Cancel" />

    you can try this c# code

    protected void Btnsubmit_Click(object sender, EventArgs e)
    this.Button1.Attributes.Add("OnClick", "self.close()");

    Response.Write("<script language='javascript'> { self.close() }</script>");

    Name : Dotnet Developer-2015
    Email Id :

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #767281
    There are couple of ways to close window, you can do it either by serverside (code behind) or by client side using scripting
    see below code where you will see how to close window using code behind code

    string display = "Your dispaly";
    ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + display + "');", true);//this will dispaly the alert box
    ScriptManager.RegisterStartupScript(this, this.GetType(), "Close_Window", "self.close();", true);//this will close the page on button click

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #767283
    Hai Krishna,
    Absolutely right as there are many ways to close the window in web application using We can use server side code using C# or client side code using JavaScript or JQuery.

    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)

  • #767299

    In your design you make this javascript code and call this function in your server side code behind.

    function CloseWindow() {

    and on cancel button click call the above function

    protected void btnCancel_Click(object sender, EventArgs e)
    Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "closePage", "window.onunload = CloseWindow();");

    Try something like above may be this may help you...

    Give respect to your work, Instead of trying to impress your boss.

    Blog :

  • Sign In to post your comments