Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delegates and UserControls

Status
Not open for further replies.

jermine

Programmer
Jun 15, 2001
59
SG
Hello to the .NET gurus out there.
I hope you guys can help me on this... im pretty new to the idea of using delegates.

I have the following user controls :
1. one that contains a calendar control - DATECONTROLLER.ASCX
2. one that contains a repeater that displays a list of item i got from the DB. - LISTMYITEMS.ASCX

The page VIEWMYITEMS.ASPX, both of the user controls are added.

What i was thinking of doing was whenever the user changes the date in the calendar control in DATECONTROLLER, the selected date is passed to the LISTMYITEMS and values displayed there also changes.

Someone told me that i should be using events and delegates but i've got no clues on how to do that.

Ive read some articles in msdn but am still having a hard time understanding it.

Can anyone of you lend a hand?

Many thanks in advance
 
1. first, declare a delegate:

Code:
public delegate void [b]DateChanged[/b](DateTime newDate);

2. then in your calendar class you declare and event:

Code:
public event [b]DateChanged[/b] [i]OnDateChanged[/i];

3. in your calendar control, where you check to see if the date has changed (probably in some event you declared), you would write:

Code:
foreach (DateChanged dc in OnDateChanged.GetInvocationList()) dc();

4. in the main class you would write:

Code:
myCalendar.OnDateChanged += new DateChanged(listMyItems.CalendarDateChanged);

5. then add this to the listmyitems class

Code:
public void CalendarDateChanged (DateTime newCalendarDate)
{
   // here write the logic for the update
}

what does it mean:
1. you declare a delegate that denotes a function returning void and taking as parameter a datetime
2. you create an event in the calendar class that uses the delegate prototype declared above
3. for each registered listener of the OnDateChanged event you call the function registered with it
4. you register your listMyItems's CalendarDateChanged function to the event called OnDateChanged of the calendar
5. you declare a function in listMyItems that matches the signature of the delegate in which you do the logic for the date update propagation

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top