如果控件处于“同一级别”,则 Page.FindControl 方法是可以的,但通常情况下,控件隐藏在其他控件中。不幸的是,您可能需要递归搜索给定页面中的所有控件。这是我用来通过给定 id 值查找控件的一段代码...
string g_Error = string.Empty;
List<Control> _infos = new List<Control>();
_infos = ProcessControls(_infos, Page, "info_");
...
public List<Control> ProcessControls(List<Control> MatchControls, Control controls, string FieldKey)
{
try
{
//get a recusive listing of all existing controls for reference
foreach (Control cControl in controls.Controls)
{
if (cControl.Controls.Count > 0)
{
//recusive call
MatchControls = ProcessControls(MatchControls, cControl, FieldKey);
}
//field rules
//must contain Fieldkey to be collected (i.e. control id = FirstName where the FieldKey is "Name")
//so first, loop through and collect controls with FieldKey
if (cControl != null)
{
if (cControl.ClientID.Contains(FieldKey))
{
MatchControls.Add(cControl);
}
}
}
}
catch (Exception ex)
{
g_Error = ex.ToString();
}
return MatchControls;
}