Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
iam writing code for button onclient event with js to print the webpage and onclick event for generating pdf and downloading it ..both must be done in a single action..
but when i cancel print which i onclient click event then oncick event is firing .

What I have tried:

 <script type="text/javascript">
         function PrintPage() 
         {
             window.print();
            return true;
         }
</script>

<asp:Button ID="Button3" runat="server"  Text="PTPDF" UseSubmitBehavior="false" OnClick="Button3_Click1"  OnClientClick ="javascript:PrintPage();"    />
       </div>


protected void Button3_Click1(object sender, EventArgs e)
        {//here is the code for pdf generation,
this code must get fired automatically after onclient click event without users action}
Posted
Updated 27-Jun-17 20:40pm
v3
Comments
Thomas Nielsen - getCore 28-Jun-17 4:18am    
Well you cannot have a single action happenig both on server and client :)
the client renders the javascript in your browser and the server (runat='server') instance of the controls is happening between request/post and response
F-ES Sitecore 28-Jun-17 4:55am    
Any attempt to automate or control printing on the client side is doomed to fail, it's just not supported.
saimanisha 28-Jun-17 5:12am    
so what is the solution if we want to provide print option to client ..
F-ES Sitecore 28-Jun-17 5:26am    
The client already has this...the print function of their browser. If they want to print something they can.
saimanisha 28-Jun-17 5:33am    
my requirement is i have written function for print which is client side and pdf generation with download option server side ..thats why i used onlick and onclientclick events..is there any possibility to work both events at a time one after other with single click .

1 solution

OnClientClick event will be fired first, it will accept boolean value ( true by default )
if the value is true, onclick event will be fired, else it wont.
it should be
OnClientClick =" return PrintPage();" 

however the window.print() method will not return any value, so its difficult to know that whether the print or cancel button is clicked.
check this, it might help you How to capture the click event on the default print menu called by Javascript window.print()? - Stack Overflow[^]
 
Share this answer
 
Comments
saimanisha 28-Jun-17 2:45am    
its working when am cancelling the print window..then onclick event is wokring .but it has to work when am not cancelling print window
Karthik_Mahalingam 28-Jun-17 2:47am    
this is how it works, what you can do is create another button (hide it) and use that for downloading your stuffs
invoke the button when the print is clicked, else not. which can be achieved by the link provided in the solution.
saimanisha 28-Jun-17 3:02am    
i didnt get whats there in that link..i need to do both things at time cleint side code and server side code must work but its not firing on server side code how do i make it possible.if u have any idea in any other way then tell me.

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