Programatically Raise Event in WebUserControl

I am not sure why this seems to be so difficult but this is my goal:
I have a webusercontrol with a calendar control in it.  When the calendar control unloads (the Unload event of the calendar control) I have written code to test for a condition.  If that condition exists, I want to raise a custom event to and handle it on the parent page.  I don't want the custom event to be tied to a click event or any other event.
I simply want to programatically raise a custom event from the webusercontrol and handle it in the parent page.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alfred A.Commented:

Use delegates,

something like this as an example:

webusercontrol1.customevent += new EventHandler(customeventfunction);

I hope this helps.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alfred A.Commented:

By the way, don't forget to add the event handler 'customereventfunction' for example

public void customereventfunction()
   /// Do something here!
create an event in webusercontrol -
public event EventHandler CustomEvent;
then raise it from your webusercontrol unload event like
if (CustomEvent != null)
CustomEvent(this, new EventArgs());
in parentpage hook up the custome event like -
yourwebusercontrol.customevent += new EventHandler(<your event handler function>);
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Alfred A.Commented:

Check this reference for further information.

Writing C# Custom Events
Find attached the steps for performing this job
I hope to be clear if not please reply.

//in your control class body as an example call it MyWebControl

        public class YourOwnEventArgs : EventArgs
            public readonly int ResultsRecordsCount;
            public readonly string Status;
 	//as an exapmle for Event arguments constructor
	    public YourOwnEventArgs(int ResultsRecordsCount, string Status)
                this.ResultsRecordsCount = ResultsRecordsCount;
                this.Status = Status;

        public delegate void YourOwnEventHandler(Object sender, YourOwnEventArgs e);
        public event YourOwnEventHandler YourOwnEvent;

//from any where in your webcontrol methods (when you want to rais the event)
YourOwnEvent.Invoke(this,new YourOwnEventArgs (0,"Failed"));

//in your from page load 
mMyWebControl.YourOwnEvent += new MyWebControl.JobStartedEventHandler(MyWebControlEventHanddler);

//finally in your form class body
        void MyWebControlEventHanddler(object sender, MyWebControl.YourOwnEventArgs  e)
//do your own code

Open in new window

tfountainAuthor Commented:
Alfred1 thnaks for the first write answer.
SameerJagdale thanks for the first full example.  I had previously done exactly what you had but did not have one like of code: if (CustomEvent != null) .  Once I implamented that in worked fine.

Thanks all
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.