In my last post I have asked feedback of my code.Today I have also crated a simple dummy project.If you think that now I have basic knowledge of delegates and events, then please tell me some difficult task for delegates and events. Below is my code-
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DelegatesAndEvents.aspx.cs" Inherits="WebProjects.DelegatesAndEvents" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btn1" runat="server" onclick="btn1_Click" Text="ONE" /><br />
<asp:Button ID="btn2" runat="server" onclick="btn2_Click" Text="TWO" /><br />
<asp:Button ID="btn3" runat="server" onclick="btn3_Click" Text="THREE" /><br />
<asp:Label ID="lblText" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
using System;
namespace WebProjects
{
public partial class DelegatesAndEvents : System.Web.UI.Page
{
public delegate void MyDelegate(string val);
public event MyDelegate myEvent;
protected void Page_Load(object sender, EventArgs e)
{
myEvent += SetLabelValue;
}
public void SetLabelValue(string val)
{
lblText.Text = val;
}
protected void btn1_Click(object sender, EventArgs e)
{
if(myEvent!=null)
{
string value = "button one is clicked.";
myEvent(value);
}
}
protected void btn2_Click(object sender, EventArgs e)
{
if (myEvent != null)
{
string value = "button two is clicked.";
myEvent(value);
}
}
protected void btn3_Click(object sender, EventArgs e)
{
if (myEvent != null)
{
string value = "button three is clicked.";
myEvent(value);
}
}
}
}
Thanks in advance.