Consuming user control custom events Part 10704:33

  • 0
Published on January 19, 2018

Text version of the video

Slides

All ASP .NET Text Articles

All ASP .NET Slides

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic

In Part 106 of this video series, we discussed about raising custom events from a user control. Please watch part 106, before proceeding.

In this video, we will discuss about
1. Consuming custom events of the user control
2. Understanding the importance of, checking if the event is null, before raining the event. We skipped discussing this, when we were discussing about raising custom events in Part 106.
protected virtual void OnCalendarVisibilityChanged(CalendarVisibilityChangedEventArgs e)
{
// NULL check
if (CalendarVisibilityChanged != null)
{
CalendarVisibilityChanged(this, e);
}
}

For the HTML and code samples used in the demo please visit my blog at the following link

Consuming custom event “CalendarVisibilityChanged”
To consume the event, there are 2 simple steps.
Step 1:
Create an event handler method as shown below. The method signature must match the signature of the “CalendarVisibilityChangedEventHandler” delegate. Notice that, in the event handler method, we are retrieving event data using “IsCalendarVisible” property.
protected void CalendarUserControl1_CalendarVisibilityChanged(object sender, CalendarVisibilityChangedEventArgs e)
{
Response.Write(“Calendar Visible = ” + e.IsCalendarVisible.ToString());
}

Step 2: Register event handler method “CalendarUserControl1_CalendarVisibilityChanged()” with “CalendarVisibilityChanged” events of the “CalendarUserControl” using “+=” as shown below. Do this, in the Page_load() event of “WebForm1”. To unregister we can use “-=”.
protected void Page_Load(object sender, EventArgs e)
{
CalendarUserControl1.CalendarVisibilityChanged +=
new CalendarVisibilityChangedEventHandler(CalendarUserControl1_CalendarVisibilityChanged);
}

That’s it. Run the project and click on the calendar image to toggle the display, the custom event will be raised and handled. You should see a message “Calendar Visible = true” or “Calendar Visible = false” depending on the visibility of the calendar control.

Understanding the importance of, checking if the event is null, before raising the event
Now comment the line that registers event handler method in the Page_Load() event. Run the application and click on the image button. Nothing happens and also we don’t get any run time errors.

Now comment the line that checks for null in “OnCalendarVisibilityChanged()” method as shown below.
protected virtual void OnCalendarVisibilityChanged(CalendarVisibilityChangedEventArgs e)
{
// NULL check
//if (CalendarVisibilityChanged != null)
//{
CalendarVisibilityChanged(this, e);
//}
}

Run the application and click on the image button. You should get a “NullReferenceException”. The exception is due to CalendarVisibilityChanged() being null. So, if there are no subscribers for the event, that is, if there are no event handler methods registered with CalendarVisibilityChanged event, and if we try to raise the event, we get the exception. To avoid this it is always better to check for null, before raising the event.

In the next video, we will discuss about raising another custom event from CalendarUserControl.

https://cafeadobro.ro/

https://www.stagebox.uk/wp-includes/depo10-bonus10/

https://iavec.com.br/

Enjoyed this video?
"No Thanks. Please Close This Box!"