简体   繁体   English

c#查找控件和控件参考

[英]c# find controls and control reference

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. 我将使用xmlhttp递归加载此页面,因此我无法使用它的id引用该表。 Is there something to the effect of this.Page.Controls[2] ? 有什么效果吗this.Page.Controls[2] or Controls["Tables"][0] ? Controls["Tables"][0]

I tried using the name this.Controls.Find("MyTableName", true); 我尝试使用名称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.' 但需要引用库System.Windows.Forms (我认为),但不知道如何将其添加为'using System.' intellisense can't see it. intellisense看不到它。

I looped through all the controls in this.Controls but '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 提前致谢

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. 页面上的常规HTML不是“控件”,它们只是发送到浏览器的文本。 Server controls, on the other hand, are actual .NET classes which can interact with the codebehind. 另一方面,服务器控件是可以与代码隐藏交互的实际.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. 您仍然可以通过搜索其容器的Controls集合或通过递归搜索页面来获取此控件的句柄而无需其ID。 Let's start with recursively searching the Controls collection: 让我们从递归搜索Controls集合开始:

The Controls collection only refers to immediate child controls of a given control. Controls集合仅引用给定控件的直接子控件。 Or immediate child controls of a Page. 或者对页面的直接子控件。 Those controls in turn have child controls of their own. 这些控件反过来又有自己的控制。 It represents a tree in memory. 它代表了记忆中的一棵树。

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. 您绝对不希望包含System.Windows.Forms,因为它只包含WinForms应用程序的所有代码。 That's why Visual Studio has not included it for you in a web project - you'll never need it. 这就是为什么Visual Studio没有在Web项目中为您提供它 - 您永远不需要它。 System.Web.UI has everything for web controls. System.Web.UI具有Web控件的所有功能。

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

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