且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用户控件之间的ASP.NET事件委派

更新时间:2023-12-06 12:19:58

为了使每个控件可重用和与其他控件分离,我一直采取这种方法:每个容器将知道它包含一级深度的控件,但不知道包含它的容器。要进行沟通,您在使用活动时是正确的。这提供了您的抽象层,并使您的所有控件重新使用。

To make each control reusable and decoupled from other controls, I've always taken this approach: Each container will know about the controls that it contains one level deep, but not about the container containing it. To communicate up, you are correct in using events. This provides your layer of abstraction and makes all of your controls reusable.

就是说,让我们来看看你的例子。您的 TitleControl 不包含任何内容,因此它会做的只是火灾事件。

That being said, let's take a look at your example. Your TitleControl does not contain anything, so all it will do is fire events.

您的 HeaderControl 引用您的 TitleControl 并处理其事件。如果它没有完全处理你的 TitleControl 的事件,那么它已经声明并触发它自己的事件,并传递原始发件人和事件参考

Your HeaderControl references your TitleControl and handles it's events. If it doesn't completely handle your TitleControl's events, then have it declare and fire it's own event, and pass along the original sender and event args.

在您的页面级别,您的页面将处理您的 HeaderControl 的事件。由于您的页面也是您的 TabsControl 的容器,请在您的 TabsControl 中的公共方法中为您的 HeaderControl 的事件,并传入冒泡的EventArg信息。

At your page level, your page will handle your HeaderControl's events. Since your page is also the container for your TabsControl, call a public method in your TabsControl inside the event handler for your HeaderControl's event and pass in the bubbled up EventArg information.

基本上,使用事件来泡沫,并使用公共方法或属性推动事情。

Basically, use events to bubble things up, and use public methods or properties to push things down.