Adding custom events to asp net composite custom control Part 11604: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 this video we will discuss about adding custom event to the asp.net composite custom calendar control that we have been working with from Parts 112 to 115. Please watch Parts 112 to 115 from the asp.net video tutorial before proceeding with this video. ASP.NET video tutorial link is shown below.

We have discussed about adding custom events to user controls in Parts 106 to 108. You can watch these videos from the asp.net video tutorial. Adding custom events to a composite control is similar to adding events to user controls.

There are 5 simple steps to add “DateSelected” custom event to composite custom calendar control
Step 1: Create “DateSelectedEventArgs” that will contain event data
public class DateSelectedEventArgs : EventArgs
{
private DateTime _selectedDate;

public DateSelectedEventArgs(DateTime selectedDate)
{
this._selectedDate = selectedDate;
}

public DateTime SelectedDate
{
get
{
return this._selectedDate;
}
}
}

Step 2: Create “DateSelectedEventHandler” delegate
public delegate void DateSelectedEventHandler(object sender, DateSelectedEventArgs e);

Step 3: Create “DateSelected” event.
public event DateSelectedEventHandler DateSelected;

Step 4: Create a protected virtual method to raise the event.
protected virtual void OnDateSelection(DateSelectedEventArgs e)
{
if (DateSelected != null)
{
DateSelected(this, e);
}
}

Step 5: Finally raise the event, when the date selection in the calendar changes.
DateSelectedEventArgs dateSelectedEventData = new DateSelectedEventArgs(calendar.SelectedDate);
OnDateSelection(dateSelectedEventData);

Complete custom calendar code can be found at the link below

Build the CustomControls project. In an asp.net web application add a reference to the CustomCalendar control. Drag and drop the CustomCalendar control on a webform. Right click on the control and select properties. In the properties window, click on the events icon. Notice that, the event “DateSelected” is displayed.

Double click on the “DateSelected” event. This should automatically generate an event handler method. Implement the event handler method as shown below.
protected void CustomCalendar1_DateSelected(object sender, CustomControls.DateSelectedEventArgs e)
{
Response.Write(e.SelectedDate.ToShortDateString());
}

Please run the project, and test.

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