C# 从循环中动态调用图像

C# 从循环中动态调用图像,c#,asp.net,css,image,C#,Asp.net,Css,Image,我试图制作一个简单的网站(图片,按钮,表格等),我有很多图片,但我不想一个接一个地设置它们的URL,相反,我想制作这样的东西 例如,我已经在HTML中创建了3个图像image1、image2和image3 <asp:Image ID="Image1" <asp:Image ID="Image2" <asp:Image ID="Image3" enter code here 您就快到了-使用FindControl查找每个图像控件 for (int i=1; i< 4;i+

我试图制作一个简单的网站(图片,按钮,表格等),我有很多图片,但我不想一个接一个地设置它们的URL,相反,我想制作这样的东西

例如,我已经在HTML中创建了3个图像image1、image2和image3

<asp:Image ID="Image1"
<asp:Image ID="Image2"
<asp:Image ID="Image3"
enter code here

您就快到了-使用
FindControl
查找每个图像控件

for (int i=1; i< 4;i++)
{
    Control control = Page.FindControl(string.Format("Image{0}", i));
    if (control != null)
    {
        control.ImageUrl = "(path of the folder)"+i.ToString()+".jpg";
    }
}
for(int i=1;i<4;i++)
{
Control=Page.FindControl(string.Format(“Image{0}”,i));
if(控件!=null)
{
control.ImageUrl=“(文件夹路径)”+i.ToString()+“.jpg”;
}
}

正如@efkah所指出的,如果你将这些图像控件放在一个面板中,那么使用
MyPanel.FindControl(…

你发布的代码有什么问题?我可以看到一些明显的改进,但我认为这基本上是个想法。好吧,图像[I]不是一个有效的变量它只是没有编译或者我不知道语法你需要某种类型的imagecontainer(
asp:Panel
),它是服务器端已知的,只需添加
新的HtmlImage()
到这个容器。@spirit\u我明白了。我想提供的答案会帮你解决这个问题。顺便说一句,你应该在问题中提供更多细节,因为据我所知,
图像属于
列表类型,并且已经填充了。如果是这样的话,你的代码会按预期运行。对我来说总是很好的包括错误信息、导致错误的行以及代码中变量的所有必要详细信息。我已经发布了一个带有错误信息的答案,这些图像位于panel id=“panel1”中
for (int i=1; i< 4;i++)
{
    Control control = Page.FindControl(string.Format("Image{0}", i));
    if (control != null)
    {
        control.ImageUrl = "(path of the folder)"+i.ToString()+".jpg";
    }
}