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

后端 未结 5 1952
没有蜡笔的小新
没有蜡笔的小新 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条回答
  •  旧时难觅i
    2021-01-23 12:16

    @Andrew, not to hijack my own thread but just to clarify and discuss a design approach. You have said "You never want any control to have any knowledge of anything above itself in the hierarchy..." That is a great design stategy for obvious reasons. But what about the case of a wrapper container with some sort of centralized management built in? For example, in .NET, a ScriptManager control in a nested Master Page. You would want a script manager created once only, and you would want it to be available to various controls in the application. For example, any UpdatePanel in any embedded control in the app would want to know about the existence of that ScriptManager. Is this a wrong approach?

    My design is similar - the outermost wrapper container manages object persistence, and each control can save it's state to that Manager. I am thinking now that wrapper delegates are the best design for this. Because I don't like the Parent.Parent. etc nor traversing the control IDs just to make a reference. Any comments welcome.

提交回复
热议问题