ASP.NET Cross-Communications Controls

Scenario: 2 user controls (foo.ascx and fum.ascx)

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

Any ideas?

+3
source share
5 answers
  • Add an event OnMyPropertyValueChangedto fum.ascx.
  • Add the appropriate EventHandler to foo.ascx, which stores the value of the property in a private variable.
  • Attach the foo.ascx event handler to the fum.ascx event on the_Load page.
  • Raise an event on fum.ascx Page_Load and, if necessary
  • Let foo.ascx use its own variable
+4
source

, , .

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);

, .

+1

UserControl, .

0

fum HttpContext.Current.Items [], foo .

A more reliable option is to give foo a property that the page can fill with a link to fum.

An event is more work, but architecturally better.

0
source

You can reference other user controls using FindControlon Foo Parent. This is the simplest, and you do not need to program anything in 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>
0
source

Source: https://habr.com/ru/post/1698375/


All Articles