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!
0 comments :