ASP.NET 用户控制交叉通信

发布于 2024-07-07 23:56:42 字数 125 浏览 10 评论 0原文

场景:2 个用户控件(foo.ascx 和 fum.ascx)

foo 有一个确实想从 fum 访问属性的方法。 他们生活在同一页上,但我找不到一种非常简单的方法来完成这种通信。

有任何想法吗?

The scenario: 2 user controls (foo.ascx and fum.ascx)

foo has a method that would really like to access a property from fum. They live on the same page, but I can't find a very simple way to accomplish this sort of communication.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦境 2024-07-14 23:56:42
  • 在 fum.ascx 中添加事件 OnMyPropertyValueChanged
  • 将相应的 EventHandler 添加到 foo.ascx 中,它将属性值存储在私有变量中。
  • 将 foo.ascx 的事件处理程序附加到 Page_Load 上的 fum.ascx 事件。
  • 在 fum.ascx Page_Load 上引发事件并在需要时
  • 让 foo.ascx 的方法使用它自己的变量
  • Add an event OnMyPropertyValueChanged in fum.ascx.
  • Add the corresponding EventHandler to foo.ascx which stores the property value in a private variable.
  • Attach the event handler of foo.ascx to the event of fum.ascx on Page_Load.
  • Raise the event on fum.ascx Page_Load and whenever needed
  • Let the method of foo.ascx use its own variable
天涯沦落人 2024-07-14 23:56:42

有几种方法可以解决这个问题,但最好的解决方案是尽可能解耦。

最解耦的方法是递归 findControl 方法,它遍历控件对象模型,直到找到所需的控件并返回引用。

private Control findControl(Control root, string id) 
{ 
    if (root.ID == id)
    { 
        return root; 
    } 

    foreach (Control c in root.Controls) 
    { 
        Control t = findControl(c, id); 
        if (t != null) 
        { 
            return t; 
        } 
    } 

    return null; 
}

这是另一种有点简洁的方法,尽管我不知道是否会使用它。(有点伪代码):

public FunkyUserControl : UserControl
{
    private List<UserControl> subscribedControls;

    public List<UserControl> Subscribers
    {
        get { return subscribedControls;}
    }

    public void SubscribeTo(UserControl control)
    {
        subscribedControls.Add(control);
    }
}

从 FunkyUserControl 继承两个用户控件,然后在主页面类中您可以执行以下操作:

webControl1.SubscribeTo(webControl2);
webControl2.SubscribeTo(webControl1);

现在每个控件都可以内省其订阅者集合来查找其他控件。

There are a few ways to handle this, but optimally you want a solution that is as decoupled as possible.

The most decoupled method would be a recursive findControl method that walks the control object model until it finds the control you want and returns a reference.

private Control findControl(Control root, string id) 
{ 
    if (root.ID == id)
    { 
        return root; 
    } 

    foreach (Control c in root.Controls) 
    { 
        Control t = findControl(c, id); 
        if (t != null) 
        { 
            return t; 
        } 
    } 

    return null; 
}

Here is another approach that is kinda neat, though I don't know if I'd use it.(Somewhat pseudocode):

public FunkyUserControl : UserControl
{
    private List<UserControl> subscribedControls;

    public List<UserControl> Subscribers
    {
        get { return subscribedControls;}
    }

    public void SubscribeTo(UserControl control)
    {
        subscribedControls.Add(control);
    }
}

Inherit your two usercontrols from FunkyUserControl and then in your main page class you can do:

webControl1.SubscribeTo(webControl2);
webControl2.SubscribeTo(webControl1);

Now each control can introspect its subscribers collection to find the other control.

零崎曲识 2024-07-14 23:56:42

将事件添加到连接到窗体的 UserControl。

Add an event to the UserControl that is hooked up to the form.

九八野马 2024-07-14 23:56:42

最简单的解决方案是 fum 在 HttpContext.Current.Items[] 中存储一个值,foo 稍后可以在其中读取它。

一个更强大的选项是为 foo 提供一个属性,页面可以使用对 fum 的引用来填充该属性。

活动的工作量更大,但架构上更好。

The simplest solution is for fum to store a value in HttpContext.Current.Items[], where foo can read it later.

A more robust option is to give foo a property that the the page can populate with a reference to fum.

An event is more work, but is architecturally nicer.

失与倦" 2024-07-14 23:56:42

您可以通过在 Foo 的 Parent 上使用 FindControl 来引用其他用户控件。 这是最简单的,您不需要在每个主(父)表单上进行任何编程。

'From within foo...call this code<br>
Dim objParent As Object<br>
Dim lngPropID As Long<br>

objParent = Me.Parent.FindControl("fum")<br>
lngPropID= objParent.PropID 'public property PropID on fum<br>

You can reference the other user control by using FindControl on Foo's Parent. This is the simplest and you don't need to program anything on each main (parent) form.

'From within foo...call this code<br>
Dim objParent As Object<br>
Dim lngPropID As Long<br>

objParent = Me.Parent.FindControl("fum")<br>
lngPropID= objParent.PropID 'public property PropID on fum<br>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文