开发者

ASP.Net Printing

开发者 https://www.devze.com 2023-02-22 23:28 出处:网络
Below are my ASP.Net code. I have a problem when user click on \"Print\" button in window.print() window, the system will call the onPrintPage to check the printing margin.

Below are my ASP.Net code. I have a problem when user click on "Print" button in window.print() window, the system will call the onPrintPage to check the printing margin.

How to do that? Please help.

Thanks.

Page.ClientScript.RegisterStartupScript(this.GetType(), "OnPrintPage", "window.print();", true);

private void OnPrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    System.Drawing.Printing.PageSettings oPagesettup1 = new System.Drawing.Printing.PageSettings();
    PrintDocument PrintDoc = new PrintDocument();

    if (PrintDoc.DefaultPageSettings.Margins.Right < e.PageSettings.Margins.Right || PrintDoc.DefaultPageSettings.Margins.Bottom < e.PageSettings.Margins.Bottom)
    {
         string script =开发者_如何学Python "<script language='javascript'>alert('Please set your Printer Orientation option to Landscape and your Page (Under File -> Page Setup) right margin to minimum value.')</script>";
         Page.ClientScript.RegisterClientScriptBlock(GetType(), "key", script);
     }
}


You appear to be confusing server-side & client-side scripts here.

The 2nd string in RegisterStartupScript is a key to allow you check whether this script has been registered or not.

The code in your OnPrintPage runs on the web server, so will only ever check the servers print settings, not the clients. It will also never get called as you've written it.

The DOM has very little support for print features, like checking margins. You'd need to use Javascript to manipulate it & use AJAX call backs if you needed to do anything you couldn't do.

Reference page for the clientscriptmanager: http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager%28VS.80%29.aspx


When the user decides to print your web page from the browser, the browser is the one that controls the printing process.

You cannot see or set the margin values. You can however have a CSS style that applies only to printing that you can then use to format the document differently.


You appear to be confusing server-side & client-side scripts here.

The 2nd string in RegisterStartupScript is a key to allow you check whether this script has been registered or not.

The code in your OnPrintPage runs on the web server, so will only ever check the servers print settings, not the clients. It will also never get called as you've written it.

The DOM has very little support for print features, like checking margins. You'd need to use Javascript to manipulate it & use AJAX call backs if you needed to do anything you couldn't do.

Reference page for the clientscriptmanager: http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager%28VS.80%29.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消