Solved

Can't get form to print from JavaScript function.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

948 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

19 Experts available now in Live!

Get 1:1 Help Now