简体   繁体   English

无法从另一个调用ASP.NET用户控件

[英]Unable to call an ASP.NET user control from another

I wish to access a user control's public property from another user control. 我希望从另一个用户控件访问用户控件的公共属性。 But, I am not able to access one user control within another. 但是,我无法访问另一个用户控件。 Even after using the namespace 'ASP', I am only able to see the reference to the current user control. 即使使用了命名空间“ ASP”,我也只能看到对当前用户控件的引用。

However, in a page I am able to refer to all the user controls with the ASP namespace. 但是,在页面中,我可以使用ASP名称空间引用所有用户控件。

Am I missing something or it needs to be that way? 我是否想念某些东西?

Update: Here the exact scenario - 更新:这里是确切的情况-
I have two user controls A and B. Now both of them are being used on a Page Default.aspx. 我有两个用户控件A和B。现在它们都在Page Default.aspx上使用。 There is a public proprty on control A which holds the name of the control clicked within A. I wish to access this property in control B to make some decisions. 控件A有一个公共属性,其中包含在A中单击的控件的名称。我希望访问控件B中的此属性以做出一些决定。 With the suggestions gioven by you, I understand reaching to the control by FindControl method of the container page, but how do I cast it to control A to fetch the property value when I cannot get the reference of the control A in B? 有了gioven的建议,我知道可以通过容器页面的FindControl方法到达控件,但是当我无法获取B中控件A的引用时,如何将其强制转换为控件A以获取属性值?

Thanks for the help guys! 谢谢你的帮助!

One user control should not know about another user control (with the exception of any contained child controls, of course). 一个用户控件不应该知道另一个用户控件(当然,除了包含的任何子控件之外)。 If ControlABC referenced ControlXYZ, then you could never use ControlABC on any page, unless that page also had an instance of ControlXYZ - it would significantly hurt your ability to re-use ControlABC. 如果ControlABC引用了ControlXYZ,那么您将永远无法在任何页面上使用ControlABC,除非该页面上也有ControlXYZ的实例-这将严重损害您重用ControlABC的能力。

One way to address this is to use events to "wire up" a connection between the two controls. 解决此问题的一种方法是使用事件“连接”两个控件之间的连接。 That way, ControlABC doesn't have a strong dependency on ControlXYZ - it can be re-used easily, even on pages where there is no ControlXYZ. 这样,ControlABC对ControlXYZ的依赖性就不强-即使在没有ControlXYZ的页面上也可以轻松重用。 The page can manage the connection between the two controls, adding delegates on ControlXYZ to the events on ControlABC, so the two controls can communicate. 该页面可以管理两个控件之间的连接,将ControlXYZ上的委托添加到ControlABC上的事件,以便两个控件可以通信。

There are likely other techniques for allowing your controls to interact with each other without introducing strong dependencies, which may have pros and cons, compared to events/delegates. 与事件/代理相比,可能还有其他一些技术可以使您的控件彼此交互而不引入强依赖关系,而依赖关系可能有利有弊。 Other SO members will point them out, I'm sure. 我敢肯定,其他SO成员也会指出。

That's exactly the way it needs to be. 那正是它需要的方式。

In my mind, it seems like a bad idea to couple one User Control to another like that. 在我看来,将一个用户控件与另一个这样的用户控件耦合似乎不是一个好主意。 There's no way to guarantee that both will be present on the page. 无法保证两者都将出现在页面上。

If there's no other way around it though, a User Control does have access to the Page. 如果没有其他解决方法,则用户控件确实有权访问该页面。 You can then use Page.FindControl to find the other User Control and access its public properties that way. 然后,您可以使用Page.FindControl查找另一个用户控件,并以这种方式访问​​其公共属性。

"namespace" is not the same as "scope": namespace is just a way to orden the class-definitions. “名称空间”与“范围”不同:名称空间只是一种定义类定义的方法。 Scope is what you variables (instances) you can see at a particular point in your code. 范围是您可以在代码的特定点看到的变量(实例)。

That other user control is on the same page? 该其他用户控件在同一页面上吗? (if not, then it will not work at all). (如果没有,那么它将根本无法工作)。 You will need to go from the current usercontrol up to the page. 您将需要从当前的usercontrol转到页面。 From there you can find that other usercontrol. 从那里您可以找到其他用户控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM