Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用for循环在多个图像中应用imageurl_C#_Asp.net_For Loop_Drop Down Menu_Imageurl - Fatal编程技术网

C# 使用for循环在多个图像中应用imageurl

C# 使用for循环在多个图像中应用imageurl,c#,asp.net,for-loop,drop-down-menu,imageurl,C#,Asp.net,For Loop,Drop Down Menu,Imageurl,我有以下代码: protected void Page_Load(object sender, EventArgs e) { DdlAddPhoto.DataSource = Enumerable.Range(1, 9); DdlAddPhoto.DataBind(); } protected void DdlAddPhoto_SelectedIndexChanged(object sender, EventArgs e) { string SelectedAl

我有以下代码:

protected void Page_Load(object sender, EventArgs e)
{
   DdlAddPhoto.DataSource = Enumerable.Range(1, 9);
   DdlAddPhoto.DataBind();
}
protected void DdlAddPhoto_SelectedIndexChanged(object sender, EventArgs e)
{        
   string SelectedAlbum = DdlAddPhoto.SelectedValue.ToString();        
   for (int i = 1; i < 20; i++)
   {
      string image = String.Format("Image{0}", Convert.ToString(i));
        string path = String.Format("~/images/prettyPhoto/{0}/fullscreen/{1}.jpg", SelectedAlbum, Convert.ToString(i));
        Image im = (Image)FindControl(image);
        im.ImageUrl = path.ToString();
   }                
}
为什么find控件无法工作?在中,断点给出im=null

答案如下:

我用这个:

((Image)FindControlRecursive<Image>(this,
    String.Format("Image{0}", Convert.ToString(i)))).ImageUrl = 
        String.Format("~/images/prettyPhoto/{0}/fullscreen/{1}.jpg",
            SelectedAlbum.ToString(), Convert.ToString(i));
((图像)FindControl递归(此,
String.Format(“Image{0}”,Convert.ToString(i))。ImageUrl=
String.Format(“~/images/prettypto/{0}/fullscreen/{1}.jpg”,
SelectedAlbum.ToString(),Convert.ToString(i));
并使用Antonio Bakula给出的方法:

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;
}
public static T FindControlRecursive(控件持有者,字符串controlID),其中T:Control
{
控件foundControl=null;
foreach(holder.Controls中的控件ctrl)
{
如果(ctrl.GetType()等于(typeof(T))&&
(string.IsNullOrEmpty(controlID)| |(!string.IsNullOrEmpty(controlID)&&ctrl.ID.Equals(controlID)))
{
foundControl=ctrl;
}
如果(ctrl.Controls.Count>0),则为else
{
foundControl=FindControlRecursive(ctrl,controlID);
}
if(foundControl!=null)
打破
}
返回(T)控制;
}

问题是
FindControl
对图像不起作用。

为什么要问?因为有些东西是空的,我的儿子。在第19行设置一个断点,然后逐步完成循环。我猜
FindControl
ImageUrl
SelectedAlbum
都是空的。NullReference错误的原因总是一样的。您正在调用对象的属性,但对象值为null。检查此查找控件是否工作?在断点中,im=null。为什么?仅供参考,图像没有什么特别之处。尝试在此页面顶部(在
下拉列表
之前)添加图像,然后查看
FindControl
是否有效。
((Image)FindControlRecursive<Image>(this,
    String.Format("Image{0}", Convert.ToString(i)))).ImageUrl = 
        String.Format("~/images/prettyPhoto/{0}/fullscreen/{1}.jpg",
            SelectedAlbum.ToString(), Convert.ToString(i));
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;
}