c#查找控件和控件参考

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1224447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 13:51:47  来源:igfitidea点击:

c# find controls and control reference

c#asp.netcontrols

提问by Praesagus

How do I reference controls in c# (runtime) without an ID?

如何在没有 ID 的情况下引用 c#(运行时)中的控件?

For example, I have a page with one table in it. I will be loading this page recursively using xmlhttp so I cannot refer to the table using it's id. Is there something to the effect of this.Page.Controls[2]? or Controls["Tables"][0]?

例如,我有一个页面,其中包含一张表格。我将使用 xmlhttp 递归加载此页面,因此我无法使用它的 id 引用该表。有什么影响this.Page.Controls[2]吗?或者Controls["Tables"][0]

I tried using the name this.Controls.Find("MyTableName", true);but need a reference to the library System.Windows.Forms(I think) but don't know how to add it as the 'using System.'intellisense can't see it.

我尝试使用该名称this.Controls.Find("MyTableName", true);但需要对库的引用System.Windows.Forms(我认为)但不知道如何添加它,因为'using System.'智能感知无法看到它。

I looped through all the controls in this.Controlsbut 'system.ui.web.control' does not contain a definition for '.Name'so I can only search for ID.

我遍历了所有控件,this.Controls'system.ui.web.control' does not contain a definition for '.Name'只能搜索 ID。

I am new to this and I am sure the solutions are infuriatingly simple.
Thanks in advance

我对此很陌生,我相信解决方案非常简单。
提前致谢

采纳答案by Rex M

To find a control on your page, it has to be a server control, like this:

要在页面上找到控件,它必须是服务器控件,如下所示:

<asp:Table runat="server">
   ...
</asp:Table>

Regular HTML on the page are not "controls", they are just text that gets sent to the browser. Server controls, on the other hand, are actual .NET classes which can interact with the codebehind.

页面上的常规 HTML 不是“控件”,它们只是发送到浏览器的文本。另一方面,服务器控件是可以与代码隐藏进行交互的实际 .NET 类。

You can still get a handle to this control without its ID by searching the Controls collection of its container, or by recursively searching the page. Let's start with recursively searching the Controls collection:

您仍然可以通过搜索其容器的 Controls 集合或通过递归搜索页面来获得此控件的句柄,而无需其 ID。让我们从递归搜索 Controls 集合开始:

The Controls collection only refers to immediatechild controls of a given control. Or immediate child controls of a Page. Those controls in turnhave child controls of their own. It represents a tree in memory.

Controls 集合仅指给定控件的直接子控件。或者 Page 的直接子控件。这些控件具有自己的子控件。它代表内存中的一棵树。

Here is a method to recurse down the tree from a given control and find the control by ID:

这是一种从给定控件向下递归树并通过 ID 查找控件的方法:

private Control FindControlRecursive(Control control, string id)
{
    Control returnControl = control.FindControl(id);
    if (returnControl == null)
    {
        foreach (Control child in control.Controls)
        {
            returnControl = child.FindControlRecursive(id);
            if (returnControl != null && returnControl.ID == id)
            {
                return returnControl;
            }
        }
    }
    return returnControl;
}

(Beyond the scope of this answer, this is better-done as an extension method).

(超出本答案的范围,作为扩展方法这样做更好)。

To find a control by something other than its ID, you can search by type:

要按 ID 以外的其他内容查找控件,您可以按类型搜索:

if(someControl is System.Web.UI.WebControls.Table)

Note that this is generally not a very good idea. It's not a great pattern if you end up having to search for controls this way - you should have an ID to your control, or you should have a reference to it already because it was created in code.

请注意,这通常不是一个好主意。如果您最终不得不以这种方式搜索控件,这不是一个很好的模式 - 您应该拥有控件的 ID,或者您应该已经拥有对它的引用,因为它是在代码中创建的。

However, it is simple to modify the method to recursively search for a type:

但是,修改方法以递归搜索类型很简单:

private Control FindTable(Control startFrom)
{
    foreach(Control child in startFrom.Controls)
    {
        if(child is System.Web.UI.WebControls.Table)
        {
            return child;
        }
        else
        {
            return FindTable(child);
        }
    }
    return null;
}

You could also have a generic form of this method:

您还可以使用此方法的通用形式:

private Control FindControl<T>(Control startFrom)
{
    foreach(Control child in startFrom.Controls)
    {
        if(child.GetType().IsAssignableFrom(typeof(T)))
        {
            return child;
        }
        else
        {
            return FindControl<T>(child);
        }
    }
    return null;
}

You definitely don't want to include System.Windows.Forms, as that simply includes all the code for a WinForms application. That's why Visual Studio has not included it for you in a web project - you'll never need it. System.Web.UI has everything for web controls.

您绝对不想包含 System.Windows.Forms,因为它只包含 WinForms 应用程序的所有代码。这就是为什么 Visual Studio 没有为您在 Web 项目中包含它 - 您永远不需要它。System.Web.UI 拥有 Web 控件的一切。