Click here to Skip to main content
16,004,924 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
anybody give some example programs of javascript in asp.net controls......
how to call function in asp.net button
Posted
Updated 1-Aug-11 5:28am
v2

There are two ways of doing it

1. In the ASPX markup, you can use OnClientClick attribute to wire-up a JavaScript event to an ASP.NET button.
HTML
<asp:button id="btnSubmit" onclientclick="return ValidatePage();" xmlns:asp="#unknown"></asp:button>


2. In the code-behind, you can use code like this:
C#
btnSubmit.Attributes.Add("onclick", "return ValidatePage();");
 
Share this answer
 
Comments
ponnapuhanu 1-Aug-11 13:03pm    
thank you..........
[no name] 1-Aug-11 14:50pm    
There are more than two ways to skin this cat. You could also add the event handler using client script

$("#btn").click(function(){ Do somthing });
[no name] 2-Aug-11 14:40pm    
Yes, but that requires jQuery.
You'll need to narrow it down a bit and say what you are trying to accomplish, but in general

<asp:button runat="server" onclientclick="return Foo();" />;
<script>
function Foo()
{
  // Do something
  //return false; // Don't complete postback
  //return true; // Complete postback
}
</script>
 
Share this answer
 
v2
Comments
ponnapuhanu 1-Aug-11 13:03pm    
thank you for ur help
[no name] 1-Aug-11 14:52pm    
Anyone care to own up and explain your 1 vote?
[no name] 2-Aug-11 14:42pm    
I have been recently undergoing a spate of downvotes for many of my answers. It would have been good if CP made comments mandatory for downvoting.

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