Solved

Can't get form to print from JavaScript function.

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 current…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
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.
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.

738 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