如何在 IE 中使用 asp.net 和 C# 在页面加载时更改图像的 ImageUrl 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/940293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change an image's ImageUrl attribute on page load with asp.net and C# in IE
提问by Jonathan Mayhak
I have links on a homepage that may or may not have attachments associated with them. If the links have attachments, then an icon will be placed next to the link. The icon will only be there if the link has an attachment.
我在主页上有链接,可能有也可能没有与之相关的附件。如果链接有附件,则会在链接旁边放置一个图标。该图标仅在链接有附件时才会出现。
protected void Page_Load(object sender, EventArgs e)
{
foreach (Image pic in imgAttachment)
{
int type = ds.Current_Events[index].AttachmentID;
//ds is the dataset
The foreach loop is going through each of the "Current Event" pictures on the homepage then getting the type of attachment associated with each link aka the AtachmentID. The AttachmentID can be a 0, 1, 2, or 3 which means no attachment, photo attached, video attached, or document attached repectively.
foreach 循环遍历主页上的每个“当前事件”图片,然后获取与每个链接关联的附件类型,即 AtachmentID。AttachmentID 可以是 0、1、2 或 3,分别表示不附加附件、附加照片、附加视频或附加文档。
A switch statement is then used to change the ImageUrl attribute to the corresponding picture.
然后使用 switch 语句将 ImageUrl 属性更改为相应的图片。
switch (type)
{
case 0:
break;
case 1:
pic.ImageUrl = "images/eventicons/Photo.jpg";
//changed from just "Photo.jpg"
break;
case 2:
pic.ImageUrl = "images/eventicons/Video.jpg";
//changed from just "Video.jpg"
break;
case 3:
pic.ImageUrl = "images/eventicons/Doc.jpg";
//changed from just "Doc.jpg"
break;
default:
pic.Visible = false;
break;
}
index++;
}
}
The image does not load in IE, however it does work for firefox.
该图像无法在 IE 中加载,但它适用于 Firefox。
The following is the aspx front
下面是aspx前面
<div>
<ul>
<li>
<asp:HyperLink ID="lblEvent1" runat="server">
<img src="images/bar_blank.gif" />
</asp:HyperLink>
<asp:Image ID="Image1" runat="server" />
</li>
<li>
<asp:HyperLink ID="lblEvent2" runat="server">
<img src="images/bar_blank.gif" />
</asp:HyperLink>
<asp:Image ID="Image2" runat="server" />
</li>
</ul>
</div>
采纳答案by Jonathan Mayhak
I figured out the problem.
我解决了这个问题。
The pictures that I was using were almost 500kb. I assumed the disk space size of the pictures were small because the images themselves were small but this was not true. I resized the images and got them down to around 40kb. The images now load in ie.
我使用的图片将近 500kb。我认为图片的磁盘空间很小,因为图像本身很小,但事实并非如此。我调整了图像大小并将它们缩小到大约 40kb。图像现在加载 ie。
So, there seems to be a problem in ie if you change the ImageUrl of an image control in asp.net during page load and the image file you are pointing to is large (500kb).
因此,如果您在页面加载期间更改asp.net 中图像控件的ImageUrl 并且您指向的图像文件很大(500kb),则似乎存在问题。
The solution is to make the picture itself take up less disk space by resizing it, making it a gif, ect..
解决方案是通过调整图片大小,使其成为gif等,使图片本身占用更少的磁盘空间。
回答by Scott Ivey
You should add the full path to your images. Instead of
您应该为图像添加完整路径。代替
pic.ImageUrl = "Photo.jpg";
you could do this to have ASP.Net write out the path of the photo as well...
你可以这样做让 ASP.Net 写出照片的路径......
pic.ImageUrl = "~/MyImagePath/Photo.jpg";
回答by Cragly
What does the html code look like that is being produced from your code? Could you post this as there are know issues with the way IE and firefox renders images based on how you reference your image. Would be good to see the rendering as well as your code behind.
从您的代码中生成的 html 代码是什么样的?您能否发布此信息,因为 IE 和 Firefox 根据您引用图像的方式呈现图像的方式存在已知问题。很高兴看到渲染以及背后的代码。