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() {
    window.close();
    }

    Or add Attribute to the button to close the current window
    http://www.beansoftware.com/ASP.NET-FAQ/Close-Browser-Button.aspx

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

    You can also use java script to do that

    http://stackoverflow.com/questions/2076299/how-to-close-current-tab-in-a-browser-window

  • #767275
    Hi

    you can try this code



    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    <script language="javascript" type="text/javascript">
    function closeWindow()
    {
    window.close();
    }
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="BtnSave" runat="server" OnClientClick="return closeWindow();" Text="Save" />
    <asp:Button ID="Button1" runat="server" OnClientClick="javascript:window.close();" Text="Cancel" />
    </div>
    </form>
    </body>
    </html>



    you can try this c# code


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

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

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    '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

    Thanks
    Koolprasd2003
    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 asp.net. We can use server side code using C# or client side code using JavaScript or JQuery.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #767299
    Hi,

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


    function CloseWindow() {
    window.close();
    }


    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.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


  • Sign In to post your comments