3

How can I print the page and close it automatically?

I open a new window/page with button click:

protected void ASPxButton_GenerOrd_Click(object sender, EventArgs e)
{
         string AdrUrl = "Print_Ordo.aspx?SoinId=" + Soin_Id + "&SelId=" + TempId;
        ClientScript.RegisterStartupScript(this.GetType(), "newWindow", String.Format("<script>window.open('{0}');</script>", AdrUrl));
    }

my Print_Ordo.aspx :

<head runat="server">
    <title></title>
     <script>         window.print(); window.close();</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <dx:ReportViewer ID="ReportViewer_Ordo" runat="server">
        </dx:ReportViewer>
    </div>
    </form>
</body>
</html>

and my Print_Ordo.aspx.cs:

public partial class Print_Ordo : System.Web.UI.Page
{

    string Soin_ID = "", SelectedID = ""; 
    protected void Page_Load(object sender, EventArgs e)
    {
        Soin_ID = Request.QueryString["SoinId"];
        SelectedID = Request.QueryString["SelId"];
        //ReportViewer_Ordo.Report = new XtraReport_Ordo(Convert.ToInt32(Soin_ID), SelectedID);
        //ReportViewer_Ordo.PageByPage = false;

        var xtraReport_Pricipal = new XtraReport_Ordo(Convert.ToInt32(Soin_ID), SelectedID);

        using (MemoryStream ms = new MemoryStream())
        {
            try
            {
                PdfExportOptions opts = new PdfExportOptions();
                opts.ShowPrintDialogOnOpen = true;
                xtraReport_Pricipal.ExportToPdf(ms, opts);
                ms.Seek(0, SeekOrigin.Begin);
                byte[] report = ms.ToArray();
                Page.Response.ContentType = "application/pdf";
                Page.Response.Clear();
                Page.Response.OutputStream.Write(report, 0, report.Length);
                HttpContext.Current.ApplicationInstance.CompleteRequest();
            }
            finally
            {
                ms.Close();
            }
        }


    }
}

The problem is it does not close automaticaly. I tried with:

self.close();
window.close();
top.close();

None of this works. Any ideas?

PS : as I look at it via Firebug:

<embed width="100%" height="100%" name="plugin" src="http://172.18.1.253:4001/source/Print_Ordo.aspx?SoinId=4&amp;SelId=9" type="application/pdf">

I think because of this process plugin, I can not close this window.

tshepang
  • 12,111
  • 21
  • 91
  • 136
user609511
  • 4,091
  • 12
  • 54
  • 86
  • 2
    Look at console, there shoud be an error "Scripts may not close windows that were not opened by script". This cause you can close windows by JavaScript only was opened by window.open javaScript method. – Lunik Sep 18 '12 at 13:24
  • 1
    Try putting the script in the Body tag. I don't know if that will make any difference, but it's still better than just having the call at the top of the html. – David Sep 18 '12 at 13:24
  • 1
    Check [this](http://stackoverflow.com/a/12070246/864989) answer – Mehmet Osmanoglu Sep 18 '12 at 13:42

1 Answers1

2

You can try with this - Add Focus on window before Close

<script type="text/javascript">
    window.print();
    window.onfocus = function() { window.close(); }
</script>
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51