?
Solved

Can't get form to print from JavaScript function.

Posted on 2008-10-20
3
Medium Priority
?
311 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 500 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

A Change in PHP Behavior with Session Write Short Circuit (http://php.net/manual/en/book.session.php#116217) (Winter 2014)** With the release of PHP 5.6 the session handler changed in a way that many think should be considered a bug.  See the note …
New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
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 selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

771 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