设置 .aspx 页面中复合控件中子控件的属性
我有一个复合控件 (class
),它通过 get-property 公开 asp:Label
。 是否可以通过aspx代码设置标签的Text
属性?
我想做这样的事情:
一种解决方案是将每个属性添加到复合类(如公共 LabelText),但我想设置任何子控件的任何属性。因此,随着子控件的新功能变得可用,我希望能够在我的复合控件上使用它们。所以基本上我想设置暴露的子控件的任何属性。
I've got a composite control (class
) that exposes an asp:Label
through a get-property. Is is possible to set the Text
property of the Label through aspx-code?
I'd like to do something like this:<cfw:MyCompositeControl runat="server" Label.Text="Test" />
One solution is adding each property to the composite class (like a public LabelText), but I'd like to set any property of any child control. So as new features of child controls become available, I'd like to be able to use them on my composite control. So basically I would like to set any property of the exposed child control.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用内部属性来完成此操作:
标记:
我以前从未使用简单的属性真正完成过此操作 - 通常,您使用的是集合。在我自己尝试这个示例时,在标记窗口中,Visual Studio 将允许您在
内创建多个TestLabel
实例 - 即是的,它似乎并不关心它是一个简单的属性而不是一个集合,我怀疑如果您输入多个条目,则只会产生最后一个条目。仅供参考...如果您以前没有这样做过,请准备好被智能感知惹恼。在对类进行更改后更新行为可能会令人烦恼地迟钝,您将需要重新编译,并且可能需要等待一段任意时间才能使其按预期方式运行。
You could do it with inner properties:
Markup:
I've never actually done this with a simple property before - usually, you are using collections. In playing with this example on my own, in the markup window, Visual Studio will allow you to create more than one instance of
TestLabel
inside<MyTag:MyControl>
- that is, it doesn't seem to care that it's a simple property rather than a collection, I suspect if you put in more than one entry just the last one would result.FYI... if you haven't done this before, prepare to be annoyed by intellisense. It can be annoyingly obtuse about updating the behavior after you make changes to a class, you will need to recompile and probably wait for some arbitrary amount of time before it acts the way it's supposed to.
您需要将其公开为复合控件类中的属性:-
然后您可以从服务器标记控制它,例如:-
You need to expose it as a property in the composite control's class:-
Then you can control it from the server tag like:-
您应该公开一个返回/设置标签的 Text 属性的公共属性。
MSDN
编辑:
不建议您从页面访问复合控件的所有子控件的想法:
You should expose a public property that returns/sets the Text property of the Label.
MSDN
Edit:
Your idea to access all child controls of the composite control from the page is not recommended:
MyCompositeControl.FindControl(ID)
(or extension methods) what would be very static and error-prone if you want to remove controls or change the IDs.