How can I attach a click event to a button and prevent postback?
The code I have doesn't seem to be working.
$('#btnNext').click(function() {
return false;
});
How can I attach a click event to a button and prevent postback?
The code I have doesn't seem to be working.
$('#btnNext').click(function() {
return false;
});
$('#btnNext').click(function(e) {
e.preventDefault();
});
I was working with a button and discovered that if you want to prevent the button from doing an auto-postback, you need to specify the type as "button". For example:
<button id="saveButton">Save</button>
--this will generate an auto-postback (tested in IE)
<button type="button" id="saveButton">Save</button>
--this will not
Hope this helps someone.
In Asp.net to prevent page postback on button click use preventDefault() function
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>WebForm1</title>
<script src="js/jquery-2.2.2.js" type="text/javascript"></script>
<script>
$(document).ready(function () {
$("#btnshow").click(function (e) {
e.preventDefault();
$("#Label1").show();
});
$("#btnhide").click(function (e) {
e.preventDefault();
$("#Label1").hide();
})
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="btnshow" runat="server" Text="Show" />
<asp:Button ID="btnhide" runat="server" Text="Hide" />
</form>
</body>
</html>
inside button tag, use type="Button" to prevent from post back.