This article will show you how you can add a javascript function
to a button control in asp.net using
c#. In this I have show two ways to assign the javascript function. First by
using OnClientClick and second from code behind using c#.
Some of my previous articles are as follows: Dynamically
Add TextBox Control and Detect TextBox Value in Asp.Net Using C#, Ajax
Call Using UpdatePanel Display Data On Button Click Without Refresh In Asp.Net,
C#, Dynamic
Css Menu Creation in Asp.Net Using C#.Net, jQueryUI
Tooltip Using jQuery on Textbox MouseOver in Asp.Net, Ajax
FileUpload Control In Asp.Net or Multiple FileUpload With Progress Example in
Asp.Net Using C#.Net, Simple
Login Form In Asp.Net Using C#.Net and VB.Net | How to Create Login Form in
Asp.Net Using C#.
So for this article first we will create a new asp.net application and add the below code in the page.
So for this article first we will create a new asp.net application and add the below code in the page.
First Way:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication2.WebForm3"
%>
<!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>Add
Javascript Function To Button Control in Asp.net Using c#</title>
<script language="javascript">
function
ShowClickMessage() {
alert("This
is a click event.");
return
false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Click to Execute Javascript" OnClientClick="javascript:return
ShowClickMessage();"/>
</div>
</form>
</body>
</html>
|
Here in above code I have OnClientClick event to assign the
javascript function which will show an alert message.
Second Way:
For this we will use the below html code for aspx page.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication2.WebForm3"
%>
<!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>Add
Javascript Function To Button Control in Asp.net Using c#</title>
<script language="javascript">
function
ShowClickMessage() {
alert("This
is a click event.");
return
false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Click to Execute Javascript"/>
</div>
</form>
</body>
</html>
|
Now from code behind add the below code on page load.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class WebForm3 : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
Button1.Attributes.Add("onclick", "javascript:return
ShowClickMessage();");
}
}
}
|
0 comments:
Please let me know your view