Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi
I want to call one javascript on click of one asp button.Am using below code,
<script type="text/javascript">
function clear() {
alert("Testing");
return false;
}
</script>




<asp:Button ID="btnclr" Text="Clear" class="btn" OnClientClick="clear();" runat="server" onclick="btnclr_Click" />


but when i click button nothing is happening. how can i do this?
The page should not do postback when i click that button, that is i wanted.
I guess page will do post back if i use clientscript.
Posted
Comments
Snesh Prajapati 18-Aug-14 9:19am    
Can u please update us ....what worked and mark those as answer. Thanks.
Am Gayathri 18-Aug-14 9:45am    
Need to add "return" in the onclientclick .Its working now.
Snesh Prajapati 18-Aug-14 9:52am    
Thanks for update !!

You must add "return" as

<asp:Button ID="btnclr" Text="Clear" class="btn" OnClientClick="return clear();" runat="server" onclick="btnclr_Click" />


for more...Have a look on
How to Call javascript function on button click event[^]

Thanks.
 
Share this answer
 
You need to prepend "return" in the click click method like as follows.

C#
<asp:button id="btnclr" text="Clear" class="btn" onclientclick="return clear();" runat="server" onclick="btnclr_Click" xmlns:asp="#unknown" />


Hope it fix your issue.
 
Share this answer
 
Try this....

XML
<head runat="server">
    <title></title>
    <script type="text/javascript" language="javascript">
        function Clear() {
            alert("Testing the code");
            return false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClientClick="Clear()" OnClick="btnCancel_Click" />
    </div>
    </form>
</body>
</html>



Thanks
 
Share this answer
 
Comments
Am Gayathri 18-Aug-14 9:44am    
Thanks for your reply.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900