user1729807
user1729807

Reputation:

I call a javascript function for print but doesn't work

I use below code for print from Page but doesn't work (means when i click on print button nothing happen). the function doesn't call

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

protected void print_Click(object sender, EventArgs e)
{
    btnPrint.Attributes.Add("onclick", "return printing()");
}

Upvotes: 3

Views: 970

Answers (3)

robasta
robasta

Reputation: 4701

When a user clicks the button, you are adding an attribute, not calling the printing() function.

You should add the OnClientClick attribute to the button in the button html like this:

<asp:button OnClientClick="return printing" ....

Upvotes: 0

4b0
4b0

Reputation: 22323

Try this way:

<asp:Button ID="print" runat="server" Text="Print" OnClientClick="javascript:window.print();" />

Upvotes: 3

Adil
Adil

Reputation: 148120

Add attribute in page_load event to bind the javascript event, so that the javascript is binded before you click the print button to print the page.

private void Page_Load(object sender, System.EventArgs e)
{
    btnPrint.Attributes.Add("onclick", "return printing()");
    //Your code here.
}

Upvotes: 2

Related Questions