I am looking at events and how they are structured differently between web pages and non web pages.
In a web page we must have a method that takes the event name and precedes the name with “On”. So in my example below the event is “StageCompleted”. So we need a special public method called “OnStageCompleted”. As a matter of fact, if you create a user control with a method called “OnStageCompleted” but without the event line – it doesn’t even show in intellisense. As soon as you add the line:
it shows up in intellisense in the user control in the .aspx page. It will work fine there. But where is defined. I am not even sure what is called? It isn’t a delege, or event – so what is it?
If I were to change it to “ForStageComplete”, it would build and run fine but wouldn’t work because it didn't start with "On".
For a basic event , (in dotnet), you don’t need the special “OnStageCompleted” method, you only need to do the following (if not being used on a web page)
1) A Delegate: We need a way to pass data from the event to the user of the event, which may or may not be a web page. If nothing being passed you can use the already defined delegate – EventArgs.
2) An Event: usually called handler. Something for the user to subscribe to.
3) Code to Raise the Event (execute the procedure defined by the user).
So we can define the argument array the delegate will use we could:
Then you would have a delegate which will be used by the event:
In the class we define the event that will use the delegate and in the class we can have a method that runs and will raise the event by calling the event name.
This would be called from another class something like:
This would go to a method “al2_StageCompleted” with no “OnStageCompleted” defined.
On a web page it would be something like:
But in this case the “OnStageCompleted method would need to be added to do what I was doing in my other method so a web page could call it:
My question is what is this method - it isn't the event?
Also, why do you have to use the word "On" to precede it. It isn't just a suggested way of doing it - you have to do it that way.
I am just curious as to where that is mentioned in MS Docs or other examples.
Thanks,
Tom
In a web page we must have a method that takes the event name and precedes the name with “On”. So in my example below the event is “StageCompleted”. So we need a special public method called “OnStageCompleted”. As a matter of fact, if you create a user control with a method called “OnStageCompleted” but without the event line – it doesn’t even show in intellisense. As soon as you add the line:
Code:
public event StageCompletedHandler StageCompleted;
it shows up in intellisense in the user control in the .aspx page. It will work fine there. But where is defined. I am not even sure what is called? It isn’t a delege, or event – so what is it?
If I were to change it to “ForStageComplete”, it would build and run fine but wouldn’t work because it didn't start with "On".
For a basic event , (in dotnet), you don’t need the special “OnStageCompleted” method, you only need to do the following (if not being used on a web page)
1) A Delegate: We need a way to pass data from the event to the user of the event, which may or may not be a web page. If nothing being passed you can use the already defined delegate – EventArgs.
2) An Event: usually called handler. Something for the user to subscribe to.
3) Code to Raise the Event (execute the procedure defined by the user).
So we can define the argument array the delegate will use we could:
Code:
public class StageCompletedEventArgs : EventArgs
{
private int productID;
public int ProductID
{
get { return productID; }
set { productID = value; }
}
private int stage;
public int Stage
{
get { return stage; }
set { stage = value; }
}
}
Then you would have a delegate which will be used by the event:
Code:
public delegate void StageCompletedHandler(object sender,
StageCompletedEventArgs e);
In the class we define the event that will use the delegate and in the class we can have a method that runs and will raise the event by calling the event name.
Code:
public class AssemblyLine
{
public event StageCompletedHandler StageCompleted;
public void StartAssemblyLine(int prodID)
{
for (int stage = 1; stage < 6; stage++)
{
System.Threading.Thread.Sleep(2000);
if (StageCompleted != null)
{
StageCompletedEventArgs args = new StageCompletedEventArgs();
args.ProductID = prodID;
args.Stage = stage;
// Raise the event
StageCompleted(this, args);
}
}
}
}
This would be called from another class something like:
Code:
AssemblyLine al = new AssemblyLine();
al.StageCompleted += new StageCompletedHandler(handleStage);
al.StartAssemblyLine(Int32.Parse(txtProdID.Text));
This would go to a method “al2_StageCompleted” with no “OnStageCompleted” defined.
On a web page it would be something like:
Code:
<uc1:Assembly2 ID="Assembly2" OnStageCompleted="handleStage" runat="server" />
But in this case the “OnStageCompleted method would need to be added to do what I was doing in my other method so a web page could call it:
Code:
protected void OnStageCompleted(int prodID, int stage)
{
if (StageCompleted != null)
{
StageCompletedEventArgs args = new StageCompletedEventArgs();
args.ProductID = prodID;
args.Stage = stage;
// Raise the event
StageCompleted(this, args);
}
}
My question is what is this method - it isn't the event?
Also, why do you have to use the word "On" to precede it. It isn't just a suggested way of doing it - you have to do it that way.
I am just curious as to where that is mentioned in MS Docs or other examples.
Thanks,
Tom