Banner Ad

Monday, February 3, 2014

Attach Javascript to Button

By Francis   Posted at   2:01 PM   No comments

In this tutorial I’m going to explain how to add a javascript snippet to the ASP.Net server side Button Control.

 

Code:

Copy the below code in an .aspx file:

 

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:Button runat="server" ID="btnSubmit" Text="Click Me" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>


 


Copy the below code in an .aspx.vb file:

 
 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
btnSubmit.Attributes.Add("onclick", "alert('Button Clicked');return false;")
End Sub


Explanation:


The above code add “onclick” attribute to the Server side button “btnSubmit”. The attribute value is just a javascript snippet, which just show an alert box.


 


Take a look, after the alert we specify the “return  false;” which avoid the webform to be refreshed.


 


Hope this helps!

About Francis

Francis, an Associate at Cognizant. Having 7+ Years of experience in Microsoft web technologies.

0 comments :

Please give your valuable comments to improve the contents

Connect with Us