Sunday, January 7, 2018

How To Raising Custom Events from Custom Net UserControls

How To Raising Custom Events from Custom Net UserControls


In the last few days Ive been searching books and websites to get back to "object oriented programming" I remember in my head from back time in college java and C++ courses about classes, event handling, delegates, inheritance .. etc but cant seem to get what I have in mind done in current C# development work requirements as custom electronic request forms automation, built with custom .net controls in SharePoint environment to be reused among them all when needed.

in this post Ill be showing you how to raise an event from within usercontrols native controls events and handle it with a custom code in webpage code file hosting the usercontrol.

watching Kudvenkats YouTube videos below helped a lot to understand the event handling technique by following the 5 steps to raise an event from the usercontrol

  1. Step 1: Create XxEventArgs class that will contain the event data.
  2. Step 2: Create XxChangedEventHandler delegate that raises this event. 
  3. Step 3: Create XxChanged event that is a variable of type delegate.
  4. Step 4: Create a protected virtual method to raise the event that enables the derived classes to do some additional work before the event can be raised. 
  5. Step 5: Finally raise the event, whenever the usercontrols controls is changed.
Lets say, we want to raise TimeChanged event every time the usercontrol ( from the previous post ) controls values changes. i.e, when hours TextBoxs Text is changed, minutes TextBoxs Text is changed, or time DropDownLists Index is Changed.
  1. Step 1: Create TimeChangedEventArgs class that will contain the event data:


    public class TimeChangedEventArgs : EventArgs

    {

        private string _ucTimeValue;


        // Constructor to initialize event data

        public TimeChangedEventArgs(string ucTimeValue)

        {

            this._ucTimeValue = ucTimeValue;

        }


        // Returns ucTimeValue   

        public string UcTimeValue

        {

            get { return this._ucTimeValue; }

        }

    }



  2. Step 2: Create TimeChangedEventHandler delegate that raises this event.


    public delegate void TimeChangedEventHandler(object sender, TimeChangedEventArgs e);


  3. Step 3: Create TimeChanged event that is a variable of type delegate inside the usercontrols class:


    public
    event TimeChangedEventHandler TimeChanged;


  4. Step 4: Create a protected virtual method to raise the event that enables the derived classes to do some additional work before the event can be raised.
    Checking if TimeChanged is null is a good practice as it will give error message in cases when you dont need to have a custom event handling in usercontrols host. 

    protected
    virtual void OnTimeChanged(TimeChangedEventArgs e)

        {

            if (TimeChanged != null)

            {

                TimeChanged(this, e);

            }

        }

  5. Step 5: Finally raise the event, whenever the usercontrols controls is changed:
    by assigning the following event handlers to the controls and then raising the event 
    1. When hours TextBoxs Text is changed:


        protected void hourTxtBx_TextChanged(object sender, EventArgs e)

          {

               OnTimeChanged(new TimeChangedEventArgs(this.hourTxtBx.Text + ":" + this.minTxtBx.Text + " " + timeDDL.SelectedValue));


          }

    2. When minutes TextBoxs Text is changed:


        protected void minTxtBx_TextChanged(object sender, EventArgs e)

          {

               OnTimeChanged(new TimeChangedEventArgs(this.hourTxtBx.Text + ":" + this.minTxtBx.Text + " " + timeDDL.SelectedValue));

          }

    3. When time DropDownLists Index is Changed:


        protected void timeDDL_OnSelectedIndexChanged(object sender, EventArgs e)

          {

              OnTimeChanged(new TimeChangedEventArgs(this.hourTxtBx.Text + ":" + this.minTxtBx.Text + " " + timeDDL.SelectedValue));

          }

Now to consume the usercontrol custom event:
  1.  Step 1: Create an event handler method. The method signature must match the signature of the "TimeChangedEventHandler" delegate.


    protected
    void fromTimeUC_OnTimeChanged(object sender, TimeChangedEventArgs e)

        {

            string errMsg = "";

             

            timeErrLbl.Text = (!checkTime(e.UcTimeValue , out errMsg)) ? errMsg : "";

            timeErrLbl.Visible = (!string.IsNullOrEmpty(errMsg))

        }

  2. Step 2: Register the created event handler method to the usercontrol OnTimeChanged event:


  <uc1:jpTimeChooser35UC OnTimeChanged="fromTimeUC_OnTimeChanged" ID="fromTimeUC"  runat="server" />




Thats it ..  hope it was helpful :)

Reference:

  • kudvenkat Youtube Video ASP.NET tutorial for beginners 106
  • kudvenkat Youtube Video ASP.NET tutorial for beginners 107


visit link download

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.