简体   繁体   English

查找动态创建的控件

[英]Find dynamically created control

I'm dynamically creating an HTML table filled with devex radio list controls and adding it to the page. 我正在动态创建一个用devex单选列表控件填充的HTML表,并将其添加到页面中。

//Create the radio list
ASPxRadioButtonList radButt = new ASPxRadioButtonList();
radButt.ID = "audit-" + audType;

tableCell2.Controls.Add(radButt);
tableRow.Cells.Add(tableCell2);
auditTable.Rows.Add(tableRow);

This all works fine. 这一切都很好。
Now, inside of a callback, I want to grab that radio list and get its setting ... so I am trying this but keep getting NULL. 现在,在回调中,我想获取该单选列表并获取其设置...,所以我尝试这样做,但始终获取NULL。

ASPxRadioButtonList audRad = (ASPxRadioButtonList)Page.FindControl("audit-" + audType);

What I am missing here? 我在这里想念的是什么?

The problem is that the Page.FindControl method doesn't search all controls on the page. 问题在于, Page.FindControl方法不会搜索页面上的所有控件。 It only searches the top layer of controls. 它仅搜索控件的顶层。 You have to search through all controls on the page using Page.FindControl and Control.FindControl , probably recursively. 您必须使用Page.FindControlControl.FindControl来搜索页面上的所有控件,可能是递归的。

Another point, do you mean to find the radiobutton in the postback, or in the same request? 还有一点,您是要在回发中还是在同一请求中找到单选按钮? If you mean in the postback, than you have to regenerate the control in the postback as well, just like Aniket mentioned. 如果您指的是回发,那么您还必须在回发中重新生成控件,就像Aniket提到的那样。

According to Maarten's answer, here is a recursive FindControl solution directly picked from our Master's blog : 根据Maarten的回答,这是直接从Master博客中选择的递归FindControl解决方案:

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; 
} 

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

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