Solved

Can't get form to print from JavaScript function.

Posted on 2008-10-20
3
304 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
  • 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction (All good things must come to an end (http://en.wikipedia.org/wiki/All_Good_Things...)) The original MySQL API (http://php.net/manual/en/book.mysql.php) has gone away, deprecated by PHP in Version 5.5, and removed from PHP in all curre…
A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

706 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now