用户控件之间的ASP.NET事件委派 [英] ASP.NET Event delegation between user controls

查看:125
本文介绍了用户控件之间的ASP.NET事件委派的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET页面上给出以下控件层次结构:

Give the following control hierarchy on a ASP.NET page:


  • 页面


    • HeaderControl      (用户控制


      • TitleControl     (服务器控制

      • Page
        • HeaderControl       (User Control)
          • TitleControl       (Server Control)

          我试图在TitleControl中引发一个事件(或一些通知),这些事件发生在页面水平。然后,我想(可选)在页面codebehind 注册一个事件处理程序,该代码将采用 EventArgs 并修改示例中的TabsControl以上。需要注意的是,这种设计将允许我将这些控件放入任何页面,并使整个系统无缝连接,如果事件处理程序被连接起来。解决方案不应该涉及调用 FindControl(),因为它成为一个强关联。如果在包含页面中没有定义处理程序,则TitleControl仍然会引发该事件,但未处理。

          I'm trying to raise an event (or some notification) in the TitleControl that bubbles to the Page level. Then, I'd like to (optionally) register an event handler at the Page codebehind that will take the EventArgs and modify the TabsControl in the example above. The important thing to note is that this design will allow me to drop these controls into any Page and make the entire system work seamlessly if the event handler is wired up. The solution should not involve a call to FindControl() since that becomes a strong association. If no handler is defined in the containing Page, the event is still raised by TitleControl but is not handled.

          我的基本目标是使用基于事件的编程,以便可以将用户控件彼此分离。 TitleControl的事件在某些情况下才被提出,这似乎是(在我看来)首选方法。但是,我似乎找不到一个干净地实现这一点的方法。

          My basic goal is to use event-based programming so that I can decouple the user controls from each other. The event from TitleControl is only raised in some instances, and this seemed to be (in my head) the preferred approach. However, I can't seem to find a way to cleanly achieve this.

          这是我的(糟糕)尝试:

          Here are my (poor) attempts:


          1. 使用HttpContext.Current.Items


          添加EventArgs到TitleControl上的Items集合,并在TabsControl上选择它。这是有效的,但是由于两个控件之间的连接并不明显,因此很难解读。

          Add the EventArgs to the Items collection on TitleControl and pick it up on the TabsControl. This works but it's fundamentally hard to decipher since the connection between the two controls is not obvious.



        • 而不是传递事件,在TitleControl中直接在容器页面上查找一个函数,如:
          Page.GetType()。GetMethod(TabControlHandler)。Invoke(Page,EventArgs);
          这将工作,但方法名称必须是一个常量所有页面实例将不得不逐字地定义。

          Instead of passing events, look for a function on the container Page directly within TitleControl as in: Page.GetType().GetMethod("TabControlHandler").Invoke(Page, EventArgs); This will work, but the method name will have to be a constant that all Page instances will have to defined verbatim.


        • 我敢肯定,我过分的思考了这一点,必须有一个更漂亮的解决方案,使用代表团,但我似乎无法想象。任何想法?

          I'm sure that I'm over-thinking this and there must be a prettier solution using delegation, but I can't seem to think of it. Any thoughts?

          推荐答案

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

          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.

          这篇关于用户控件之间的ASP.NET事件委派的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆