views:

368

answers:

5

Hi, 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 a listings to a pdf when the user clicks on it; as you can note the script calls a function from the behind code.

The problem is that when I coded this it happens that when I go this page it prints to pdf when is loading, I thought it would wait for a click but instead it performs the printing.

What is the problem? thanks in advance.

PD. I'm working with VS2005 and for the pdf creation I use iTextSharp.

A: 

Its a bit hard to tell exactly what is happening from your code snippet, but I would assume that you are outputting the pdf file to the response stream as part of the print() function. This is will display the pdf on load because the code behind is evaluated at render time (just before it sends the code to the browser) instead of being evaluated on the client.

To get the behavior you would like the best method is to create a http handler file (ashx) that will accept any required arguments as request variables on the url and will then output the pdf to the stream.

You can then just create a href to the ashx file with the correct arguments to dispaly the document.

Dean Maughan
+1  A: 

You want to use a HyperlinkControl and wire up the OnClick event handler to your code behind Print method. When the user then clicks on the link, the page will post back and the OnClick event gets raised. From there you can print your PDF.

In your example the Print() function is getting evaluated when the page loads. Anything withing <% %> and <%= %> brackets gets evaluated at render time.

The best way to do what you want is to have a link on your page that redirects to a ASHX page that then outputs the PDF directly into the response stream. There should be plenty of examples on how this is done around the place (Google ashx). The link would look like this:

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

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

So if you property was:

protected int CurrentPageId { get { return 4; } }

It would write the following to your page:

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

Rob

Robert Wagner
+1  A: 
<a href="javascript:void(0);" onclick=<% Print(); %> title="Print listings">Print</a>

You can't do this. I know what you're thinking but it's impossible and rather (excuse me) naive.

nmiranda wrote:

The problem is that when I coded this it happens that when I go this page it prints to pdf when is loading, I thought it would wait for a click but instead it performs the printing.

This makes absolute sense. What you need to do is to put call on Print() function into different webpage and then call this webpage by AJAX in your onclick event.

lubos hasko
A: 

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

You just have to change your link to be <a href="printPDF.aspx" title="Print listings">Print</a>

If you have to pass parameters to it then i would do change the link to be <a href="javascript:void(0);" onlick="javascript:GoToPrint();" title="Print listings">Print</a>

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

Avitus
A: 

Finally I made it, I have to clear that first of all I made all my reports using another web page, according to the responses I received I realized that my apprroach to call a report within the same page using a href was not correct.

So, I analized Robert Wagner 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 url calling an ashx where I produce my report, the ashx receives different parameters according to some issues and it worked very nice.

It executes the script and shows the pdf in another windows exactly as I needed.

Thanx.

nmiranda