تقييمات الطلاب
( 5 من 5 )
١ تقييمات
فيديو شرح Consuming user control custom events Part 107 ضمن كورس ASP.net شرح قناة kudvenkat، الفديو رقم 107 مجانى معتمد اونلاين
Text version of the video
http://csharp-video-tutorials.blogspot.com/2013/01/consuming-user-control-custom-events.html
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation1
Slides
http://csharp-video-tutorials.blogspot.com/2013/08/part-107-consuming-user-control-custom.html
All ASP .NET Text Articles
http://csharp-video-tutorials.blogspot.com/p/free-aspnet-video-tutorial.html
All ASP .NET Slides
http://csharp-video-tutorials.blogspot.com/p/aspnet-slides.html
All Dot Net and SQL Server Tutorials in English
https://www.youtube.com/user/kudvenkat/playlists?view1&sortdd
All Dot Net and SQL Server Tutorials in Arabic
https://www.youtube.com/c/KudvenkatArabic/playlists
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
http://csharp-video-tutorials.blogspot.com/2013/01/consuming-user-control-custom-events.html
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.