Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Events and delegates in c# - Part 108

Suggested Videos
Part 105 - Using user controls on a webform
Part 106 - Raising custom events from user controls
Part 107 - Consuming user control custom events

In this video we will discuss about raising another custom event from CalendarUserControl. If you have not watched Parts 106 and 107, I would strongly encourage you to do so before continuing with this video. In this part, we will discuss about raising DateSelected custom event, whenever, the user selects a "Date" from the "CalendarUserControl".



The following are the steps to raise this event
Step 1: Create "DateSelectedEventArgs" class that will contain the 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 "OnDateSelection" to raise the event.
protected virtual void OnDateSelection(DateSelectedEventArgs e)
{
    if (DateSelected != null)
    {
        DateSelected(this, e);
    }
}

Step 5: Finally raise the event, whenever a date is selected from the CalendarUserControl. 
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
    txtDate.Text = Calendar1.SelectedDate.ToShortDateString();
    DateSelectedEventArgs dateSelectedEventData = new DateSelectedEventArgs(Calendar1.SelectedDate);
    OnDateSelection(dateSelectedEventData);

    // Rest of the code
}

Finally, here is the complete CalendarUserControl code, with both the custom events. 
1. "DateSelected" event
2. "CalendarVisibilityChanged" event

public partial class CalendarUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Calendar1.Visible = false;
        }
    }

    protected void ImgBtn_Click(object sender, ImageClickEventArgs e)
    {
        if (Calendar1.Visible)
        {
            Calendar1.Visible = false;
            CalendarVisibilityChangedEventArgs calendarVisibilityChangedEventData =
                new CalendarVisibilityChangedEventArgs(false);
            OnCalendarVisibilityChanged(calendarVisibilityChangedEventData);
        }
        else
        {

            Calendar1.Visible = true;
            CalendarVisibilityChangedEventArgs calendarVisibilityChangedEventData = new CalendarVisibilityChangedEventArgs(true);
            OnCalendarVisibilityChanged(calendarVisibilityChangedEventData);
        }
    }

    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        txtDate.Text = Calendar1.SelectedDate.ToShortDateString();
        DateSelectedEventArgs dateSelectedEventData = new DateSelectedEventArgs(Calendar1.SelectedDate);
        OnDateSelection(dateSelectedEventData);

        Calendar1.Visible = false;
        CalendarVisibilityChangedEventArgs calendarVisibilityChangedEventData = new CalendarVisibilityChangedEventArgs(false);
        OnCalendarVisibilityChanged(calendarVisibilityChangedEventData);
    }

    public string SelectedDate
    {
        get
        {
            return txtDate.Text;
        }
        set
        {
            txtDate.Text = value;
        }
    }

    public event DateSelectedEventHandler DateSelected;

    public event CalendarVisibilityChangedEventHandler CalendarVisibilityChanged;

    protected virtual void OnCalendarVisibilityChanged(CalendarVisibilityChangedEventArgs e)
    {
        if (CalendarVisibilityChanged != null)
        {
            CalendarVisibilityChanged(this, e);
        }
    }

    protected virtual void OnDateSelection(DateSelectedEventArgs e)
    {
        if (DateSelected != null)
        {
            DateSelected(this, e);
        }
    }
}

public class DateSelectedEventArgs : EventArgs
{
    private DateTime _selectedDate;

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

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

public class CalendarVisibilityChangedEventArgs : EventArgs
{
    private bool _isCalendarVisible;

    public CalendarVisibilityChangedEventArgs(bool isCalendarVisible)
    {
        this._isCalendarVisible = isCalendarVisible;
    }

    public bool IsCalendarVisible
    {
        get
        {
            return this._isCalendarVisible;
        }
    }
}

public delegate void CalendarVisibilityChangedEventHandler(object sender, CalendarVisibilityChangedEventArgs e);

public delegate void DateSelectedEventHandler(object sender, DateSelectedEventArgs e);

To consume the event, create an event handler method as shown below.
protected void CalendarUserControl1_DateSelected(object sender, DateSelectedEventArgs e)
{
    Response.Write("Selected Date = " + e.SelectedDate.ToShortDateString());
}

Register event handler method "CalendarUserControl1_DateSelected()" with "DateSelected" event 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.DateSelected += 
    new DateSelectedEventHandler(CalendarUserControl1_DateSelected);
}

Here is the complete code for WebForm1.aspx.cs, that consumes both the custom events.
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CalendarUserControl1.CalendarVisibilityChanged +=
            new CalendarVisibilityChangedEventHandler(CalendarUserControl1_CalendarVisibilityChanged);

        CalendarUserControl1.DateSelected +=
            new DateSelectedEventHandler(CalendarUserControl1_DateSelected);
    }

    protected void CalendarUserControl1_CalendarVisibilityChanged(object sender, CalendarVisibilityChangedEventArgs e)
    {
        Response.Write("Calendar Visible = " + e.IsCalendarVisible.ToString());
    }

    protected void CalendarUserControl1_DateSelected(object sender, DateSelectedEventArgs e)
    {
        Response.Write("Selected Date = " + e.SelectedDate.ToShortDateString());
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write(CalendarUserControl1.SelectedDate);
    }
}

Hopefully, by now we should have a good understanding of events & delegates, and raising and consuming custom events. If there are any questions or feedback, please feel free to leave a comment.

4 comments:

  1. sir you are just Great... I just Loves ur videos.. U r Awesome !!!

    ReplyDelete
  2. You are great. You work is inspiration

    ReplyDelete
    Replies
    1. Hi Amit,

      Thank you very much for taking time to give feedback. I am really glad you found these videos useful.

      I have organised all the ASP .NET, C#, and SQL Server video tutorials in to playlists, which could be useful to you.
      http://www.youtube.com/user/kudvenkat/videos?view=1&flow=grid

      Tips to effectively use my youtube channel.
      http://www.youtube.com/watch?v=nT9uF09RMkw

      If you want to receive email alerts, when new videos are uploaded, please feel free to subscribe to my youtube channel.
      http://youtube.com/kudvenkat

      If you like these videos, please click on the THUMBS UP button below the video.

      May I ask you for a favour. I want these tutorials to be helpful for as many people as possible. Please free to share the link with your friends and family who you think would also benefit from them.

      Good Luck
      Venkat

      Delete
  3. Very Awesome and useful video series.
    These videos are very well explained and made all my basics clear which is really helpful to become a good .Net professional.

    Thank you Venkat Sir!

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.