Event not occuring, returning null

  • Thread starter Thread starter JC
  • Start date Start date
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");
}
}
}
************************************************************
 
JC said:
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

I don't see how you're expecting it to work. You're creating one
instance of RaiseEvent in the designer section, as a local variable,
and adding the event handler to it.

When the button is clicked, you're then creating *another* instance of
RaiseEvent (which will therefore have no handlers) and raising the
event within it.
 
Hi Jon,

I was wondering if you could expain something to me - I'm really rather
new to delgates and events. I now have a class that fires and event, and
a web page subscribes to it. How would I go about adding another class,
to also subscribe to that event?

Thanks

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
James Crane said:
I was wondering if you could expain something to me - I'm really rather
new to delgates and events. I now have a class that fires and event, and
a web page subscribes to it. How would I go about adding another class,
to also subscribe to that event?

I don't know much about ASP.NET to be honest, but I'd suggest going
back to the basics and learning about events and delegates from a
straight C#/.NET point of view. I don't have any links to tutorials off
the top of my head, but I'm sure you should be able to find loads
without too much hassle. When you've worked out what "basic" events and
delegates are about, there may be some extra features of ASP.NET which
hook into them in a particular way, but it'll be a lot easier to
understand by then.
 
Back
Top