Pass an Event from one control to another


Just a little trick but it will help some developers I presume ;-)

I have 2 controls A and B on a webpage.

I raise an  event in control A (like a Dropdown list change for example), and I want to send a sign to the control B that something new happened from the control A.

Don't bother to write something in one of the controls, just write something like that in your code on the main page (Example in VB for a dropdownlist SelectIndexchanged)

'Declare the controls A and B

Protected WithEvents ControlA_ID as MyNameSpace.ControlA
Protected WithEvents ControlB_ID as MyNameSpace.ControlB

' Now in the Page_load event
' Reference the Dropdownlist in the Control A

Dim MyDropdown as DropDownList = CType(ControlA_ID.Findcontrol("MyDropDown_ID"), DropDownList)

' Now delegate the SelectedIndexChanged event to a ControlB subroutine

AddHandler MyDropDown.SelectedIndexChanged, AddressOf ControlB_ID.MySub

In the Control B you just have to write now the public subroutine to handle the event.
You can start with something like

Public Sub MySub(ByVal src as Object, e as EventArgs)
.
.
.

End Sub

The interesting thing is that the native event attached to the Control A Dropdown fire before the one I created.



No Comments