raising custom events to allow web user controls to intercommunicate
I have 2 web user controls, both inherit the same base class which exte开发者_开发知识库nds UserControl. I want to raise an event on one and the other should be aware of it. both are on the same page however the 2nd control never handles the custom event i raised on the 1st one ! any ideas ? please just point to an implementation of possible (BTW, i'm googling it since morning but no luck !)
You can easily do the following:
1) create an event in control 1.
2) create a public method in control 2, that gets called if the event in control 1 is raised
3) subscribe to the event of control 1 (where you instanciate them, e.g. another control, a content page or a master page)
4) inside the event handler from 3), call the method created in 2)
5) do the other thing around, if control 2 should communicate with control 1
This all require server-side execution. I'm sure there might be also client-side solutions.
Edit after comment:
There are only two possible communication ways I can think of:
1) the controls don't know each other and therefore can't communicate directly. => you need a third control/page/masterpage/whatever (database, service etc) to enable the communication
2) the controls reference each other and therfore can communicate directly => very bad OOP. If you use this, you do not understand OOP at all and should consider your job
Feel free to chose one of the options.
You can Raise Events from User Controls using Delegates
Look Here for an example
精彩评论