Thursday, March 22, 2012

Suppress Postback on ImageButton

Is there a straightforward way to suppress the postback on an imagebutton? I want it to only fire the client onclick event.

Thanks!

If you return false from your OnClientClick event, then the postback will not be invoked.

Thanks for the response. Hmmm...didn't seem to work in this example. Did I miss something?
==============================

<%@. Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ImageButton ID="ImageButton1" runat="server" Height="20px" Width="20px"
Style="z-index: 101; left: 180px; position: absolute; top: 128px"
OnClientClick="javascript:ImageClick(this)"/>
</div>
</form>
<script>
function ImageClick(varObj)
{
alert(varObj.tagName)
return false
}
</script>
</body>
</html>

==============================


Thanks for the response. Hmmm...didn't seem to work in this example. Did I miss something?
==============================

<%@. Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ImageButton ID="ImageButton1" runat="server" Height="20px" Width="20px"
Style="z-index: 101; left: 180px; position: absolute; top: 128px"
OnClientClick="javascript:ImageClick(this)"/>
</div>
</form>
<script>
function ImageClick(varObj)
{
alert(varObj.tagName)
return (false)
}
</script>
</body>
</html>

==============================


Just returnfalsefrom your ImageClick() javascript function, or do something like:

OnClientClick="javascript:ImageClick(this); return false;"/>
Got it. Works great. Thanks!

0 comments:

Post a Comment