[Webinar] Streamline your web hosting managementRegister Today


Programatically Raise Event in WebUserControl

Posted on 2010-04-06
Medium Priority
Last Modified: 2013-12-17
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.
Question by:tfountain
LVL 21

Accepted Solution

Alfred A. earned 1000 total points
ID: 29978039

Use delegates,

something like this as an example:

webusercontrol1.customevent += new EventHandler(customeventfunction);

I hope this helps.
LVL 21

Expert Comment

by:Alfred A.
ID: 29978376

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

public void customereventfunction()
   /// Do something here!
LVL 13

Assisted Solution

SameerJagdale earned 1000 total points
ID: 29978479
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>);
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

LVL 21

Expert Comment

by:Alfred A.
ID: 29979176

Check this reference for further information.

Writing C# Custom Events


Expert Comment

ID: 30013824
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


Author Closing Comment

ID: 31711667
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question