Solved

Can't get form to print from JavaScript function.

Posted on 2008-10-20
3
310 Views
Last Modified: 2012-06-27
Hello,
    This is a simple one, but I don't know what I'm doing incorrectly. I'm trying to print a web form (.aspx) using a server side button control. I have the following code on the aspx page on the Page_Load event.

            If Not IsPostBack Then
                btnPrint.Attributes.Add("onclick", "PrintPage();")
            End If


Which should add onclick="PrintPage" to the client side html that is eventually generated.

In the source, I have the following in the html header.

<head runat="server">
    <title>Page Title</title>
    <script language ="javascript">
        Function PrintPage(){      
                                        window.print                            }
   
    </script>
</head>

I have viewed source on the html and it looks correct, but the page errors out whenever I click on the button. I have no idea why. Any ideas are appreciated. Thanks,
0
Comment
Question by:dro_law
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
M3mph15 earned 125 total points
ID: 22764911
Why not add it in the client side code :
<asp:Button ID="btnPrint" runat="server" Text="Print" OnClientClick="PrintPage();" />
and then you have the javascript function:
<script type="text/javascript">
function PrintPage()
{
         window.print();
}
</script>
0
 
LVL 3

Author Comment

by:dro_law
ID: 22769479
Thanks for the suggestion. I wasn't using onClientClick (as you suggest), so I thought that may have been the problem. However, there is no difference. The page errors out in exactly the same manner. The only thing I can think of is that I have hit a security restriction of some kind. I even tried changing the function to display a simple alert and it refuses to work. It's like it can't see the javascript function at all, but I don't see why that would be the case.
0
 
LVL 3

Author Comment

by:dro_law
ID: 22772231
K, I got it. OnClientClick is right, but it didn't like the semi-colon after the PrintPage call. So, I tried it like so, and it worked just fine

function PrintPage(){
                if (window.print)
                window.print()
            }


<asp:Button ID="btnPrint" runat="server" Text="Print This Page" OnClientClick="PrintPage()" /><br />
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Using Quotation Marks in PHP This question (http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28217211.html) seems to come up a lot for developers who are new to PHP.  And it got me thinking, "How can we explain the rule…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question