在aspx页面上查找动态添加的控件

发布于 2024-10-07 07:37:12 字数 193 浏览 4 评论 0 原文

我有一个 asp 页面“A”,并且创建了一个用户控件“B”

当我执行 A.Controls.Add(B) 时,我想创建一个元素,例如在控件“B”内定义的 TableRow ,通过代码隐藏不可见。

当查看“A”的控件时,我似乎找不到该表行。

有什么提示吗?

我确信我缺少的东西非常简单。

谢谢。

I have an asp page "A" and I've created a user control "B"

When I do A.Controls.Add(B), I would like to make an element, say a TableRow which is defined inside control "B", invisible via code behind.

When looking through Controls of "A", I cannot seem to find that table row.

Any hints?

I'm sure it is something really simple I'm missing.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

才能让你更想念 2024-10-14 07:37:12

您应该将可修改且可从外部访问的控件公开为属性。
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).

唐婉 2024-10-14 07:37:12

您可以使用 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!

源来凯始玺欢你 2024-10-14 07:37:12

我喜欢使用递归扩展方法 - 无论嵌套等如何,效果都很好。您可以破解它以返回具有特定 ID 的控件。

Param Parent 可以是任何控件,包括 Webform 或 UserControl :-)

    //Recursively get all the Controls within parent
    public static IEnumerable<Control> GetAllControls(this Control parent)
    {
        foreach (Control control in parent.Controls)
        {
            yield return control;
            foreach (Control descendant in control.GetAllControls())
            {
                yield return descendant;
            }
        }
    }

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 :-)

    //Recursively get all the Controls within parent
    public static IEnumerable<Control> GetAllControls(this Control parent)
    {
        foreach (Control control in parent.Controls)
        {
            yield return control;
            foreach (Control descendant in control.GetAllControls())
            {
                yield return descendant;
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文