用户控件之间ASP.NET事件代表团 [英] ASP.NET Event delegation between user controls

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

问题描述

给下面的控制层次结构中的ASP.NET页面:


  • 网页

    • HeaderControl      (用户控制的)

      • TitleControl      (服务器控件的)


    • TabsControl      (用户控制的)

    • 其它控件


我试图提高在TitleControl的气泡在级别的事件(或通知)。然后,我想在页面codebehind ,将采取 EventArgs的来(可选)注册一个事件处理程序并修改TabsControl在上面的例子。需要注意的重要一点是,这样的设计可以让我这些控件拖放到任意页面,使整个系统无缝地工作,如果事件处理程序接线。该解决方案不应涉及到的FindControl()的调用自认为成为一个很强的关联。如果没有处理程序中包含页面定义,该事件仍然TitleControl提出,但没有被处理。

我的基本目标是使用基于事件的编程,这样我可以相互分离的用户控件。从TitleControl该事件仅在某些情况下提出的,这似乎是(在我的脑海)在preferred方法。不过,我似乎无法找到一个干净地实现这一目标。

下面是我的(差)的尝试:


  1. 使用HttpContext.Current.Items


      

    EventArgs的添加对项目集合上TitleControl,并把它捡起来就TabsControl。这工作,但它是从根本上很难破译,因为这两个控件之间的连接并不明显。



  2. 使用反射


      

    不是传递活动,期望在中TitleControl直接容器页面中的函数:
       Page.GetType()实现getMethod(TabControlHandler)调用(页,EventArgs的);
      这将工作,但该方法的名称必须是一个常数,所有的实例必须逐字定义。



我敢肯定,我过想这和必须有使用代表团prettier解决方案,但我似乎无法想起来。有什么想法?


解决方案

为使每个控制可重复使用和其他控制分离,我一直采取这种做法:每个容器会知道,它包含一个深层次的控制,但不是关于包含它的容器中。沟通了,你是在用正确的事件。这提供了您的抽象层,使所有控件的可重用性。

话虽这么说,让我们来看看你的榜样。你的 TitleControl 不包含任何东西,因此,所有会做的很火的事件。

HeaderControl 引用您的 TitleControl 并处理它的事件。如果它不完全处理您的 TitleControl 事件,那么的有它宣布并启动它自己的事件的,并沿原始发件人通过,并事件的args。

在你的网页级别,页面将处理您的 HeaderControl 的事件。由于您的网页也是你的 TabsControl ,请致电您的 TabsControl 事件处理程序中的公共方法容器的 HeaderControl 的事件,并通过在冒泡EventArg信息。

基本上,使用事件冒泡的事情了,和使用公共方法或属性推下来。

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

  • Page
    • HeaderControl       (User Control)
      • TitleControl       (Server Control)
    • TabsControl       (User Control)
    • other controls

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.

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. Using HttpContext.Current.Items

    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.

  2. Using Reflection

    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.

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.

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.

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天全站免登陆