title="Print listings">Print

Print pdf on page load

I have this code in my aspx page;

<a href="javascript:void(0);" onclick=<% Print(); %> title="Print listings">Print</a>

      

which presents a link to print lists in pdf when the user clicks on it; as you can notice that the script is calling the function from the code behind.

The problem is, when I was coding this, it happens that when I go to this page, it prints to pdf on load, I thought it would wait for a click, but instead it does print.

What is the problem? thanks in advance.

PD. I am working with VS2005 and I am using iTextSharp to generate PDF.

0


source to share


5 answers


You want to use HyperlinkControl and hook up an OnClick event handler to your code behind the Print method. When the user then clicks on the link, the page will be posted back and the OnClick event will be raised. From there, you can print the PDF file.

In your example, the Print () function gets the score on page load. Anything related to <%%> and <% =%> brackets are evaluated at render time.

The best way to do what you want is to have a link on your page that redirects to the ASHX page, which then outputs the PDF directly to the response stream. There should be many examples of how this is done around the site (google ashx). The link will look like this:

<a href="PrintMyPdf.ashx?PageID=<%= CurrentPageId %>">Print</a>

      

This will pass the "PageID" parameter to ashx, from which you can determine which page you want to print. The <% =%> syntax evaluates the expression (in this case, the property is behind in your code, but it could be a method or forumla) and inserts the result into that part of the page.



So, if you have a property:

protected int CurrentPageId { get { return 4; } }

      

The next page says the following:

<a href="PrintMyPdf.ashx?PageID=4">Print</a>

      

Rob

+1


source


<a href="javascript:void(0);" onclick=<% Print(); %> title="Print listings">Print</a>

      

You cannot do this. I know what you are thinking, but this is not possible and rather (sorry) naive.



nmiranda wrote:

The problem is that when I coded it it happens that when I go to this page, it prints to pdf on load, I thought it would wait for a click, but instead it does print.

This is the absolute meaning. You need to make a call to the Print () function on another webpage , and then call that webpage with AJAX on the onclick event .

+1


source


It's a little tricky to tell exactly what is coming from your code snippet, but I would suggest that you are outputting the pdf file to the response stream as part of the print () function. This will render the pdf on load because the code behind is being evaluated at render time (just before the code is sent to the browser) instead of being evaluated on the client.

To get the behavior, your best bet would be to create an http (ashx) handler file that will take any required arguments as request variables on the url and then output the pdf to a stream.

Then you can just create a href for the ashx file with the correct arguments to send the document.

0


source


You just need to have another page that prints the PDF document. Let's say the page is printPDF.aspx

You just need to change your link to be

<a href="printPDF.aspx" title="Print listings">Print</a>

      

If you need to pass parameters, then I would change the link to

<a href="javascript:void(0);" onlick="javascript:GoToPrint();" title="Print listings">Print</a>

      

Script:

<script>
    function GoToPrint()
    {
        window.location = 'printPDF.aspx?var1=x&var2=y';
    }
</script>

      

0


source


Finally I did this, I have to understand that first of all I did all my reports on another web page, according to the answers I received, I realized that my aproroach to call a report on one page using href was not correct.

So, I analyzed Robert Wagner's suggestion and I tried this approach;

protected void btnPrint_Click(object sender, EventArgs e)
{
    ...
    string url = GetUrlWithParameters();

    string reportscript = "<script language='JavaScript'>" +
        "window.open('" + url + "', 'CustomPopUp', " +
        "'width=400, height=400, resizable=yes, scrollbars=yes')" +
        "</script>";

    Page.RegisterStartupScript("reportscript", reportscript);
    ...

      

In GetUrlWithParameters () I construct and type url where I create my report, ashx gets different parameters according to some problem and it worked very well.

It executes the script and shows the pdf in other windows exactly as I needed it to.

Thanx.

0


source







All Articles