0

I have a web page with a #resultsDiv that's created dynamically depending on a backend query for dollar value and quantity expected with CSS page breaks inserted between each part number's data. There can be very many, or just a few pages in the #resultsDiv depending on the filter the user sets. It's designed so our inventory team can print one page per part number for locations in our warehouse where they might find that part.

My problem is that when I click on the button to print the pages in the div (code below), it only shows the first page (which is the only page I don't want to print). Conversely, if I right-click & print, all the pages are available to be printed.

I've read through these SO questions and tried most of their solutions to no avail: window.print() is not printing the whole page

window.print() only prints the “viewable” part of the screen

ScriptManager.RegisterStartupScript code not working - why?

I've also tried some things suggested here:

Advanced CSS Printing — Using CSS Page Breaks

For reference, here's the MSDN entry for ScriptManager:

ScriptManager.RegisterStartupScript Method

Here's the click handler:

    protected void btnPrint_OnClick(object sender, EventArgs e)
    {
        if (!(ViewState["PartsToPrint"] is DataTable dt)) return;
        DataView dv = new DataView(dt);
        DataTable dtDistinctPartNumbers = dv.ToTable(true, "partNumber");
        foreach (DataRow row in dtDistinctPartNumbers.Rows)
        {
            //pull out the data as needed from the datatable rows
            string partNumber = row["partnumber"].ToString();
            if (!dbf.IncrementPrintCount(GetSelectedOrganization(), partNumber))
            {
                setStatus(false, "Error saving print count");
            }
        }
        ScriptManager.RegisterStartupScript(resultsDiv, GetType(), resultsDiv.UniqueID, "window.print();", true);
    }

I've also tried:

ScriptManager.RegisterStartupScript(this, typeof(Page), "Print", "window.print();", true);

ScriptManager.RegisterStartupScript(resultsDiv, typeof(Page), "Print", "window.print();", true);

And other variants including the ASP rendered Panel (div) selector (see CSS below), so I'm running out of ideas.

Here's the relevant CSS:

@media print {
    body, html, #wrapper, #resultsDiv, 
#ContentPlaceHolder1_resultsDiv {
        height: 100%;
        width: 100%;
    }
    p.pagebreakhere {
        display: block;
        page-break-before: always;
    }
}

I need the click event to allow me to print all the pages, not just the first page. Possibly put more simply, give me the exact same dialog I get with a right-click & Print. What am I missing?

delliottg
  • 3,950
  • 3
  • 38
  • 52

1 Answers1

0

Well, this turned out to be easy as long as you're OK with doing some client side code (I'm OK with this solution).

In the .aspx page, modify:

<asp:Button runat="server" ID="btnPrint" OnClick="btnPrint_OnClick" Text="Print for recount"/> 

To:

<asp:Button runat="server" ID="btnPrint" OnClick="btnPrint_OnClick" Text="Print for recount" OnClientClick="javascript:window.print();"/>

The "right click & Print" dialog pops up which allows the user to print all the pages, and the OnClick event still runs to do the backend updating of print counts.

There may be a more elegant solution to this, but I'm under time constraints (inventory is later this week), so I'm willing to move forward with this solution.

delliottg
  • 3,950
  • 3
  • 38
  • 52