在aspx页面上查找动态添加的控件
我有一个 asp 页面“A”,并且创建了一个用户控件“B”
当我执行 A.Controls.Add(B) 时,我想创建一个元素,例如在控件“B”内定义的 TableRow ,通过代码隐藏不可见。
当查看“A”的控件时,我似乎找不到该表行。
有什么提示吗?
我确信我缺少的东西非常简单。
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该将可修改且可从外部访问的控件公开为属性。
MSDN
但请考虑不要公开整个控件本身,但仅具有文本框的文本属性,因为这可以防止无意和不可预测的行为。
用户控件应该封装复杂性并且应该(大部分)可重用。您为使用用户控件的控件(例如页面)提供的控制越多,它们的可重用性就越差,并且越容易出错。
例如,如果您构建了一个带有用户名文本框和密码文本框的登录控件,则提供名为用户名和密码的属性而不是返回文本框本身是有意义的。每个属性都返回各自的 Textbox-Text 值。
在您的 TableRow 示例中,它的用途是什么?您应该公开一个具有有意义名称的属性来控制其可见性(如果该行包含标题,则 getter/setter 将返回/设置实际表行的可见状态)。
当然,如果您想访问 UserControl 的属性,您需要引用它。如果您在页面上找到源代码时遇到问题,您应该提供源代码。这取决于您创建和添加它的位置。通常,您会在 FindControl 的控件://msdn.microsoft.com/en-us/library/system.web.ui.control.namingcontainer.aspx" rel="nofollow">NamingContainer(在 GridView 的 TemplateColumn 中定义的控件的 NamingContainer是 GridViewRow 本身)。
You should expose the controls that are modifiable and accessible from outside as properties.
MSDN
But consider not to expose the whole control itself but only f.e. a Text-Property of a Textbox, because that prevents from unintentional and unpredictable behaviour.
A usercontrol should encapsulate complexity and should be (mostly) reusable. The more control you offer to the controls(f.e. the page) that use the usercontrol, the less reusable and the more error-phrone they are.
If you for example have built a Login-Control with a Textbox for the Username and a Textbox for the Password, it makes sense to offer properties named Username and Password instead of returning the Textboxes itself. The properties each return the respective Textbox-Text value.
In your example with the TableRow, what is its purpose? You should expose a property with a meaningful name to control its visibility(f.e.
ShowTitle
if the row contains a title, the getter/setter would return/set the actual tablerow's visible state).Of course you need a reference to your UserControl if you want to access its properties. You should provide sourcecode if you have problems on finding it on the page. It depends on where you have created and added it. Normally you will find controls with FindControl on the NamingContainer (f.e. the NamingContainer of a control defined in a GridView's TemplateColumn is the GridViewRow itself).
您可以使用 Page 中的
FindControl
方法通过其 Id 查找特定控件,我不确定您是否想找到您添加的 UserControl 'A' 或其中的控件,只要您知道 Id 并且该表,您应该能够使用相同的方法找到其中的控件row 是通过 xml 声明的,它设置了 runat="server" 属性。
希望有帮助!
you can use the
FindControl
method from Page to find a specific control by its Id,I'm not sure if you want to find that UserControl 'A' you've added or a control inside it, you should be able to use the same method to find the control inside as long as you know the Id and if this table row was declared through the xml, it has the runat="server" attribute set.
hope it helps!
我喜欢使用递归扩展方法 - 无论嵌套等如何,效果都很好。您可以破解它以返回具有特定 ID 的控件。
Param Parent 可以是任何控件,包括 Webform 或 UserControl :-)
I like to use a recursive extension method - works nicely regardless of nesting etc. You can hack it to return a control with a particular ID.
Param Parent can be any control, including a Webform or a UserControl :-)