J
JC
Hi,
I am about to take a cricket bat to my PC, so in the interests of
stopping this terrible act of cruelty to a fairly innocent Dell
machine, anyones help here would be SO appreciated.
I have a web app, with a RaiseEvent.cs in a Components foler - in this
file I am trying to raise an event. I also have a page WebForm1.aspx,
and here I am trying to subscribe to this event - BUT!!! everytime I
run the code, it tells me that the event is null i.e. I get my custom
message 'Event is empty ' I am now utterly confused how to raise event
- so thanks again for your help
*****************************RaiseEvent.cs****************************
using System;
namespace EventRaisingDemoCode
{
/// <summary>
/// Summary description for RaiseEvent.
/// </summary>
public class TestEventArgs : EventArgs
{
public string ReachedNumber
{
get {return "IRRITATION!!!";}
}
}
public delegate void MyEventDelegate(TestEventArgs e);
public class RaiseEvent
{
public event MyEventDelegate MyEvent;
public RaiseEvent()
{
//
// TODO: Add constructor logic here
//
}
public void CallRaiseEvent()
{
TestEventArgs oe = new TestEventArgs();
FireEvent(oe);
}
protected virtual void FireEvent(TestEventArgs e)
{
if(MyEvent == null)
{
System.Web.HttpContext.Current.Response.Write("Event is empty");
}
else
{
MyEvent(e);
}
}
}
}
********************************************************************
************************WebForm1.aspx*******************************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace EventRaisingDemo
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
EventRaisingDemoCode . RaiseEvent oRaiseEvent = new
EventRaisingDemoCode . RaiseEvent();
oRaiseEvent.MyEvent += new EventRaisingDemoCode .
MyEventDelegate(this.WriteText_EventRaised);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
EventRaisingDemoCode . RaiseEvent oRaiseEvent = new
EventRaisingDemoCode . RaiseEvent();
oRaiseEvent.CallRaiseEvent();
}
private void WriteText_EventRaised(EventRaisingDemoCode.TestEventArgs
e)
{
Response.Write("This is the first method raised by the event");
}
}
}
************************************************************
I am about to take a cricket bat to my PC, so in the interests of
stopping this terrible act of cruelty to a fairly innocent Dell
machine, anyones help here would be SO appreciated.
I have a web app, with a RaiseEvent.cs in a Components foler - in this
file I am trying to raise an event. I also have a page WebForm1.aspx,
and here I am trying to subscribe to this event - BUT!!! everytime I
run the code, it tells me that the event is null i.e. I get my custom
message 'Event is empty ' I am now utterly confused how to raise event
- so thanks again for your help
*****************************RaiseEvent.cs****************************
using System;
namespace EventRaisingDemoCode
{
/// <summary>
/// Summary description for RaiseEvent.
/// </summary>
public class TestEventArgs : EventArgs
{
public string ReachedNumber
{
get {return "IRRITATION!!!";}
}
}
public delegate void MyEventDelegate(TestEventArgs e);
public class RaiseEvent
{
public event MyEventDelegate MyEvent;
public RaiseEvent()
{
//
// TODO: Add constructor logic here
//
}
public void CallRaiseEvent()
{
TestEventArgs oe = new TestEventArgs();
FireEvent(oe);
}
protected virtual void FireEvent(TestEventArgs e)
{
if(MyEvent == null)
{
System.Web.HttpContext.Current.Response.Write("Event is empty");
}
else
{
MyEvent(e);
}
}
}
}
********************************************************************
************************WebForm1.aspx*******************************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace EventRaisingDemo
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
EventRaisingDemoCode . RaiseEvent oRaiseEvent = new
EventRaisingDemoCode . RaiseEvent();
oRaiseEvent.MyEvent += new EventRaisingDemoCode .
MyEventDelegate(this.WriteText_EventRaised);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
EventRaisingDemoCode . RaiseEvent oRaiseEvent = new
EventRaisingDemoCode . RaiseEvent();
oRaiseEvent.CallRaiseEvent();
}
private void WriteText_EventRaised(EventRaisingDemoCode.TestEventArgs
e)
{
Response.Write("This is the first method raised by the event");
}
}
}
************************************************************