Troy Mitchel
Troy Mitchel

Reputation: 1810

2 clicks to print page from code-behind asp.net

I am using Asp.Net and have a printer friendly button on the web page. I have a printer friendly css file for printing which is fired from code behind. This works but it has to be clicked twice initially, then only once after that. Anyone know why?

   Protected Sub PrintPartialButton_Click(sender As Object, e As EventArgs) Handles PrintPartialButton.Click
    With PrintPage
        .Attributes.Remove("media")
        .Attributes.Remove("href")
        .Attributes.Add("media", "print")
        .Attributes.Add("href", "printer_friendly.css")
    End With

    PrintPartialButton.Attributes.Add("onclick", "window.print(); return false;")
End Sub

Upvotes: 2

Views: 1976

Answers (1)

Justin
Justin

Reputation: 2103

You have to click it twice initially because you are attaching the "onclick" event on your first click. You need to attach it on page_load or page_init. Seems like you would be better off not using a server control for this or just add "OnClientClick='window.print();return false;'" to the server control's markup.

Upvotes: 3

Related Questions