Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends! I've written the following code in Markup, where I'm getting error when "btnPrint" button is clicked..

Error:
"Microsoft JScript runtime error: Unable to get value of the property 'style': object is null or undefined"

javascript:
JavaScript
function PrintReport() {
    window.print();
    document.getElementById('btnPrint').style.visibility = 'hidden';
}


in table row:

ASP.NET
<asp:Button ID="btnPrint" runat="server" Text="Print"  OnClientClick="PrintReport()"/>


Can anyone answer the solution plz..

Regards,
Ravi kamesh
Posted
Updated 17-Jul-12 21:37pm
v2
Comments
kk2014 18-Jul-12 3:41am    
hi ravi kamesh,

i applied ur code to my app. it works perfectly here.

kkakadiya
prk@cp 18-Jul-12 4:32am    
Have u taken print out??when Print button is clicked, error is coming..

That is because there is no element with id 'btnPrint' in your html.
Even if you write id="btnPrint" in your server control (button) it wont end up with that same id when the page is rendered.

be ware of the order in your javascript

JavaScript
function PrintReport() {
    var btnId = '<%=btnPrint.ClientID %>'; // find the server generated clientId

    // Hide before you call window.print()
    document.getElementById(btnId).style.visibility = 'hidden';

    window.print();
}
 
Share this answer
 
v2
Comments
bbirajdar 18-Jul-12 4:31am    
Correct answer.. +5..The client id of the control has to be used and not the server ID
StianSandberg 18-Jul-12 4:43am    
take a look at your generated html source and check if the javascript produces the same id as the actual id of your button
prk@cp 18-Jul-12 4:41am    
yep...now working properly..ThankUUUUUU
StianSandberg 18-Jul-12 4:44am    
glad i could help :)
HTML
<style type="text/css" media="screen">
        .button
        {
            display: block;
        }
</style>
<style type="text/css" media="print">
        .button
        {
            display: none;
        }
</style>

and ur button code
ASP.NET
<asp:button id="btnPrint" runat="server" text="Print" onclientclick="javascript:window.print();" CssClass="button" />
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900