简体   繁体   English

如何找到放置在面板内的控件?

[英]How do I find a control I placed inside a panel?

I have a button inside a Panel. 我在Panel中有一个按钮。 I'm trying to find the id of the button, but my code doesn't work: 我正在尝试找到按钮的ID,但我的代码不起作用:

protected void pnl_nocutomer_Load(object sender, EventArgs e)
{
    Button btn;
    btn = this.FindControl("btn_clear") as Button;

    Page.LoadComplete += new EventHandler(Page_LoadComplete);
    string LanguageID = Globals.GetSuitableLanguage(Page);
    if (LanguageID == "ar")
    {
        btn.Text = Globals.Translate("Ok", LanguageID);
    }
}

FindControl only searches the object's container, in your case, the page. FindControl仅搜索对象的容器(在您的情况下为页面)。 But the button your looking for is contained in a panel that is contained by the page. 但是您要查找的按钮包含在页面包含的面板中。

You'll need to do a recursive search to find it. 您需要进行递归搜索才能找到它。

你尝试做一个YourPanelName.FindControl()怎么样?

I like using the function found on CoddingHorror . 我喜欢使用CoddingHorror上的函数。 Just stick your panel as the root and the id of the control you are looking for. 只需将您的面板作为根和您正在寻找的控件的ID。 Like the previous answer mention, this one uses recursion to find the control you are looking for. 像之前的回答一样,这个使用递归来查找您正在寻找的控件。 Use this code. 使用此代码。

button btn = (Button)FindControlRecursive(pnl_nocustomer, "btn_clear");

private Control FindControlRecursive(Control root, string id) 
{ 
    if (root.ID == id)
    { 
        return root; 
    } 

    foreach (Control c in root.Controls) 
    { 
        Control t = FindControlRecursive(c, id); 
        if (t != null) 
        { 
            return t; 
        } 
    } 

    return null; 
} 

It's an old thread but it is a top search result so it's worth putting some notes here for others to find: 这是一个老线程,但它是一个顶级的搜索结果,所以值得在这里写一些注释供其他人查找:

x.FindControl searches all the controls that have x as the naming container. x.FindControl搜索所有具有x作为命名容器的控件。 That means that if you have a Control inside a Panel, FindControl WILL find it. 这意味着如果你在Panel中有一个Control,FindControl会找到它。 FindControl is NOT restricted to only finding controls with matching ID that are listed in x.Controls. FindControl不仅限于查找x.Controls中列出的匹配ID的控件。 You could think of FindControl as effectively doing a recursive search, that just doesn't look within child (or child of child) controls that are INamingContainers. 您可以将FindControl视为有效地执行递归搜索,而不是在子级(或子级子级)控件中查看INamingContainers。

INamingContainers are controls that use their content as a template to create zero, one or multiple copies of the controls within it. INamingContainers是使用其内容作为模板在其中创建零个,一个或多个控件副本的控件。 eg Repeater, etc 例如中继器等

So, if you have a control within a Repeater then Page.FindControl will not find it. 因此,如果你在Repeater中有一个控件,那么Page.FindControl将找不到它。 The above recursive function, using Page as the root, will find the first instance of that control from the repeater template. 上面的递归函数,使用Page作为根,将从转发器模板中找到该控件的第一个实例。 If you know you will only have one repeater, then OK sure, go for it (but you might as well start the recursive search on the repeater control then, not on the whole page) 如果你知道你只有一个中继器,那么确定,去吧(但你也可以在转发器控件上开始递归搜索,而不是在整个页面上)

When you want references to controls that are within naming containers that aren't the page, then it's better to get the reference from the "sender" of an event, eg the OnLoad of the control you are after. 当您想要引用位于不是页面的命名容器内的控件时,最好从事件的“发送者”获取引用,例如您所使用的控件的OnLoad。 If your "sender" is the naming container (the repeater template) then you can use FindControl on that to move down to the control that you are after. 如果您的“发件人”是命名容器(转发器模板),那么您可以使用它上的FindControl向下移动到您所在的控件。

If there were no repeaters involved, then the original poster probably actually just had a spelling mistake (control is not called btn_clear in the ASCX file). 如果没有涉及转发器,那么原始海报可能实际上只是拼写错误(控制在ASCX文件中不称为btn_clear)。 Would need to see the markup. 需要看标记。 In this case it's probably why they never followed up, but others who think that FindControl won't find a control within a Panel are probably going to find this page so that's why I have posted this follow-up. 在这种情况下,这可能是他们从未跟进的原因,但是其他认为FindControl无法在Panel中找到控件的人可能会找到这个页面,这就是我发布这个后续行动的原因。

将runat =“server”添加到该特定元素

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

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