在我的网络应用程序中,我有一个数据列表。我需要在每3个项目后插入一些图像。代码如下:
for (int i = 1; i <= dlHomeContent.Items.Count; i++)
{
if (i > 0 && i % 3 == 0)
{
imgPath = "~/Advertisement/homeadvt" + adCnt + ".jpg";
if (File.Exists(Server.MapPath("/Advertisement/homeadvt" + adCnt + ".jpg")))
{
HtmlGenericControl divAdv = new HtmlGenericControl();
divAdv = (HtmlGenericControl)dlHomeContent.Items[i - 1].FindControl("divAdv");
divAdv.Attributes.Remove("class");
divAdv.Attributes.Add("class", "show-control");
Image imgAdvt = new Image();
imgAdvt = (Image)dlHomeContent.Items[i - 1].FindControl("imgAdvt");
imgAdvt.ImageUrl = imgPath;
}
adCnt++;
}
}
图像放在根项目下的Advertisement文件夹中。这在我的本地机器上工作得很好。但当我将其发布到远程服务器(在亚马逊上设置)时,图像没有显示出来。可能是什么原因?
答案 0 :(得分:0)
for (int i = 1; i <= dlHomeContent.Items.Count; i++)
{
if (i > 0 && i % 3 == 0)
{
imgPath = "~/Advertisement/homeadvt" + adCnt + ".jpg";
if (File.Exists(Server.MapPath("/Advertisement/homeadvt" + adCnt + ".jpg")))
{
HtmlGenericControl divAdv = new HtmlGenericControl();
divAdv = (HtmlGenericControl)dlHomeContent.Items[i - 1].FindControl("divAdv");
divAdv.Attributes.Remove("class");
divAdv.Attributes.Add("class", "show-control");
Image imgAdvt = new Image();
imgAdvt = (Image)dlHomeContent.Items[i - 1].FindControl("imgAdvt");
imgAdvt.ImageUrl = Server.MapPath("/Advertisement/homeadvt" + adCnt + ".jpg");
}
adCnt++;
}
}
您应该在与项目文件夹映射后保存图像位置。