Nested User Controls - how to best get a reference to an ancestor control

后端 未结 5 2005
没有蜡笔的小新
没有蜡笔的小新 2021-01-23 11:31

I realize that a whole lot of code cannot fit here, but I am asking for general direction or pointer.

I have .NET user controls nested six deep for an interactive gadget

5条回答
  •  我在风中等你
    2021-01-23 11:51

    I think it really depends on your usage of the controls. If the control only serves one business need and is just used on one page to satisfy a use case, then there is no need to go crazy with interfaces and properties. I don't see anything wrong with the parent.parent.parent.etc idea; used it many times myself. I would wrap it up in a property though so that whenever you need it you can call this.MyGreatGreatGrandFatherProperty. It will be more readable and if your hierarchy depth changes you only have one place to fix.

    Now if it is a generic component that you don't control the use of, or will be needed in many use cases, then you need to add properties for the parent to tell you who they are to keep de-coupled.

提交回复
热议问题