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
Now to consume the usercontrol custom event:
Thats it .. hope it was helpful :)
Reference:
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
- Step 1: Create XxEventArgs class that will contain the event data.
- Step 2: Create XxChangedEventHandler delegate that raises this event.
- Step 3: Create XxChanged event that is a variable of type delegate.
- 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.
- Step 5: Finally raise the event, whenever the usercontrols controls is changed.
- Step 1: Create TimeChangedEventArgs class that will contain the event data:public class TimeChangedEventArgs : EventArgs{private string _ucTimeValue;// Constructor to initialize event datapublic TimeChangedEventArgs(string ucTimeValue){this._ucTimeValue = ucTimeValue;}// Returns ucTimeValuepublic string UcTimeValue{get { return this._ucTimeValue; }}}
- Step 2: Create TimeChangedEventHandler delegate that raises this event.public delegate void TimeChangedEventHandler(object sender, TimeChangedEventArgs e);
- Step 3: Create TimeChanged event that is a variable of type delegate inside the usercontrols class:
public event TimeChangedEventHandler TimeChanged; - 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);}} - 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 - 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));} - 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));} - 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:
- 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))} - 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
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.