PC Review


Reply
Thread Tools Rate Thread

c# Event raising problem (revised)

 
 
Tina
Guest
Posts: n/a
 
      27th Aug 2006
(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;
}
}


 
Reply With Quote
 
 
 
 
=?Utf-8?B?U2F6aWQgS2hhbg==?=
Guest
Posts: n/a
 
      27th Aug 2006

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.

"Tina" wrote:

> (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;
> }
> }
>
>
>

 
Reply With Quote
 
Tina
Guest
Posts: n/a
 
      28th Aug 2006
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

"Sazid Khan" <(E-Mail Removed)> wrote in message
news:0A737E51-FBEE-4A5A-AF18-(E-Mail Removed)...
>
> 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.
>
> "Tina" wrote:
>
>> (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;
>> }
>> }
>>
>>
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C# Event Raising Problem Tina Microsoft ASP .NET 2 27th Aug 2006 07:20 PM
Raising an Event Mark Microsoft C# .NET 2 17th Jan 2005 04:39 PM
App sporadically hangs while raising event while processing system.threading.timer event Dan Microsoft Dot NET Compact Framework 6 25th Oct 2004 09:38 PM
App sporadically hangs while raising event while processing system.threading.timer event Dan Microsoft C# .NET 6 25th Oct 2004 09:38 PM
Raising Event =?Utf-8?B?U2FudGhvc2hp?= Microsoft Dot NET 5 25th Feb 2004 11:32 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:35 AM.