Peter
Peter

Reputation: 38455

button with no postback?

Well i have a asp.net page where i have a button that im using to execute a JavaScript... but after the JavaScript has run the page reloads(postback) how can i avoid that?

<button onclick="printElement('content');">Print</button>

Upvotes: 39

Views: 75993

Answers (5)

Sandor de Klerk
Sandor de Klerk

Reputation: 41

You should just use an input button instead:

<input type="button" onclick="printElement('content')" value="Print" />

This doesn't NEED an form (so you can use it without) and you don't have the problem with the postback.

Upvotes: 4

user1382306
user1382306

Reputation:

Add type="button" to your <button> tag like so:

<button type="button" onclick="printElement('content');">Print</button>

This answer explains the behavior.

Upvotes: 52

Massimiliano Peluso
Massimiliano Peluso

Reputation: 26727

if you don't need a post back you should use an HTML button.

in that case you don't need override the behavior to avoid the post back and it's laighter to load (it doesn't need to be worked by the server to render back the HTML)

Upvotes: 4

anishMarokey
anishMarokey

Reputation: 11397

you need to use return false

<button onclick="printElement('content');return false">Print</button>

Upvotes: 67

Paddy
Paddy

Reputation: 33857

Alter you click event as below:

<button onclick="printElement('content');return false;">Print</button>

Upvotes: 7

Related Questions