1. ............................................................................................
public partial class testdel : System.Web.UI.Page
{
public event EventHandler<mycustArgs> Clicking;
public virtual void OnClicking(object sender, mycustArgs arg)
{
Clicking?.Invoke(this,arg);
}
protected void Page_Load(object sender, EventArgs e)
{
Clicking += clsSubscriber.OnClicking;
Button1.Click += new EventHandler(ButtonClickEvent);
Button2.Click += new EventHandler(ButtonClickEvent);
Button3.Click += new EventHandler(ButtonClickEvent);
}
void ButtonClickEvent(object sender, EventArgs e)
{
//
Response.Write((sender as Button).ID);
mycustArgs myargs = new mycustArgs();
myargs.ButtonID = (sender as Button).ID;
myargs.Message = "I am Clicked!!!";
OnClicking(this, myargs);
}
}
public class mycustArgs : EventArgs
{
public string ButtonID { get; set; }
public string Message { get; set; }
}
public class clsSubscriber
{
public static void OnClicking(object sender, mycustArgs arg)
{
HttpContext.Current.Response.Write(arg.ButtonID
+ "
" +
arg.Message);
}
}
2.............................................................
public partial class multicast : System.Web.UI.Page
{
public delegate void ProgressReporter(int percentComplete);
protected void Page_Load(object sender, EventArgs e)
{
}
//
create a delegate, assign methods to it, then invoke it.
//
This is time consuming method...so we need to know its progress.
//
so we give a delegate parameter and ask it to fire it.. to let the subscribers
know about progress.
public void HardWork(ProgressReporter pr)
{
for (int i = 0; i < 10;
i++)
{
pr(i * 10); // invoking the
delegate
System.Threading.Thread.Sleep(100);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
ProgressReporter p =
WriteProgressToConsole; // assigning methods to delegate
p += WriteProgressToFile;
HardWork(p);
}
public void
WriteProgressToConsole(int percent)
{
Response.Write("WriteProgressToConsole value: " + percent);
}
public void WriteProgressToFile(int percent)
{
Response.Write("WriteProgressToFile value: " + percent);
}
}
0 comments:
Post a Comment