c# Event raising problem (revised)

T

Tina

(my last post was incomplete so this issue is being reposted)

I have an ASP.Net C# app with a Default.aspx page with an ascx user control
on the page named EditGrid.ascx given the ID myEG on the page.


I am trying to raise a custom event in this user control passing custom data
arguments. I have done this many times in VB but this is my first time in
C#.

In the default.aspx page I have the following code to subscribe to the event
along with an empty method to handle the event:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack){

myEG.RaiseCFEvent += new
EditGrid.ChangedFieldsEventHandler(this.HandleChangedEvent); }

}


protected void HandleChangedEvent(object sender, ChangedFieldsEventArgs
e)
{
string myString = "debug";
}


The code below, in the ascx control, passes the proper arguments in the
this.OnRaiseCFEvent(myArgs); code but in the OnRaiseCFEvent method
RaiseCFEVent is always null so this.RaiseCFEvent(this,e); never executes.

Can anyone tell my why RaiseCFEvent is always null? As you can see there is
a subscriber
to the event.
thanks,
T

public partial class EditGrid : System.Web.UI.UserControl
{
public delegate void ChangedFieldsEventHandler(object sender,
ChangedFieldsEventArgs e);
public event ChangedFieldsEventHandler RaiseCFEvent;
protected virtual void OnRaiseCFEvent(ChangedFieldsEventArgs e)
{
if (this.RaiseCFEvent != null)
{
this.RaiseCFEvent(this,e);
}
}
..
..
..
ChangedFieldsEventArgs myArgs = new
ChangedFieldsEventArgs(gvRow.RowIndex, i, string1, string2);
this.OnRaiseCFEvent(myArgs);

..
..
..

}

public class ChangedFieldsEventArgs : EventArgs
{
public readonly int row, col;
public readonly string newVal, oldVal;
public ChangedFieldsEventArgs(int row, int col, string newVal, string
oldVal)
{
this.row = row;
this.col = col;
this.newVal = newVal;
this.oldVal = oldVal;
}
}
 
G

Guest

Hi Tina,

the code looks neat except for the fact : event should be assigned in the
OnInit() not OnLoad().

Move

myEG.RaiseCFEvent += new
EditGrid.ChangedFieldsEventHandler(this.HandleChangedEvent); }

in the default.aspx.cs to

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
myEG.RaiseCFEvent += new
ChangedFieldsEventHandler(this.HandleChangedEvent);
this.Load += new System.EventHandler(this.Page_Load);
}

Thanks,
Sazid.
 
T

Tina

oh, you know what, I had the subscription code after my postback test. I
just had to move it up above so I resubscribe
on each postback. And, your solution would have worked too.

Thanks for your help.
T
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

C# Event Raising Problem 2
C# MVC Razor page form 0
Revised Question on File Processing 2
Raising BubleEvents 0
Using a custom event. 6
Raising events in a control 1
Event question 4
event 3

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top