查找code一个形象背后 [英] Find a image in code behind
本文介绍了查找code一个形象背后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用find控制方法找到的设计师形象,使其可见,但我不断收到一个空
I would like to use the find control method to find a image on the designer and make it visible, but i keep getting a null
这是我的code:
foreach (ImageShow image in imageList)
{
Image Showimage = (Image)FindControl(image.imageName);
Showimage.Visible = true;
}
任何帮助将是非常美联社preciated,
在此先感谢
Any help would be much appreciated, Thanks in advance
推荐答案
的FindControl并不整个的控制层次搜索,我要presume,这是一个问题。
FindControl does not search throughout a hierarchy of controls, I presume that this is a problem.
请尝试使用下面的方法:
try using the following method :
public static T FindControlRecursive<T>(Control holder, string controlID) where T : Control
{
Control foundControl = null;
foreach (Control ctrl in holder.Controls)
{
if (ctrl.GetType().Equals(typeof(T)) &&
(string.IsNullOrEmpty(controlID) || (!string.IsNullOrEmpty(controlID) && ctrl.ID.Equals(controlID))))
{
foundControl = ctrl;
}
else if (ctrl.Controls.Count > 0)
{
foundControl = FindControlRecursive<T>(ctrl, controlID);
}
if (foundControl != null)
break;
}
return (T)foundControl;
}
用法:
Image Showimage = FindControlRecursive<Image>(parent, image.imageName);
在你的情况下,父母是这样的,例如:
In your case parent is this, example :
Image Showimage = FindControlRecursive<Image>(this, image.imageName);
您可以使用它没有身份证,然后会发现的T第一次出现:
You can use it without ID, then will find first occurrence of T :
Image Showimage = FindControlRecursive<Image>(this, string.Empty);
这篇关于查找code一个形象背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文