Suggested Videos
Part 104 - Creating user controls
Part 105 - Using user controls on a webform
Part 106 - Raising custom events from user controls
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);
}
}
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.
Part 104 - Creating user controls
Part 105 - Using user controls on a webform
Part 106 - Raising custom events from user controls
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);
}
}
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.
If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website
0 comments:
Post a Comment