C# - How to add an Event Handler on Server Side?
C# - Add Client Script Events to ASP.NET Web Server Controls
Recently in one of the interviews I was asked How you can add an event handler? Below is the ans and an example to demonstrate how to add an event handler on server side.
Ans. Using the Attributes property of server side control in asp.net.
Default.aspx
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="Default"
%>
<!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>How to add
an Event Handler on Server Side?</title>
<script type="text/javascript">
function
ShowMessage()
{ alert('hey
there!'); }
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnEnter" runat="server" Text="Click Me" />
</div>
</form>
</body>
</html>
Default.aspx.Cs
protected void Page_Load(object sender, EventArgs
e)
{
// Using the Attributes property of server side
control.
btnEnter.Attributes.Add("onMouseOver", "ShowMessage();");
}
C# - How to add an Event Handler on Server Side?
Reviewed by Ravi Kumar
on
11:28 PM
Rating:

No comments: