简体   繁体   中英

How do I find a control I placed inside a panel?

I have a button inside a Panel. I'm trying to find the id of the button, but my code doesn't work:

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. 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 . Just stick your panel as the root and the id of the control you are looking for. 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. That means that if you have a Control inside a Panel, FindControl WILL find it. FindControl is NOT restricted to only finding controls with matching ID that are listed in x.Controls. 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.

INamingContainers are controls that use their content as a template to create zero, one or multiple copies of the controls within it. eg Repeater, etc

So, if you have a control within a Repeater then Page.FindControl will not find it. The above recursive function, using Page as the root, will find the first instance of that control from the repeater template. 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. 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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