Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Consuming user control custom events - Part 107

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.

2 comments:

  1. Hi sir,
    I have a small dout here .How can button.click can hold the object of a delegate.

    ReplyDelete
  2. Hi Pragim,

    In this part of video you are saying as we should register the event before it gets fired.
    So, you have registered this event(CalenderVisibilityChanged) in Page_Load as Page_Load event happens before the control events(i.e. ImageButton click here).

    My question is as Page_Prerender event is also takes place before the control events(i.e. ImageButton click here), then why it is not working in case of PagPrerender event.

    ReplyDelete

It would be great if you can help share these free resources