Wednesday, September 23, 2015

Events and delegates

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.


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