如何在ASP的img控件中显示的图像? [英] How to display image in img ASP control?
问题描述
我想的img控件来显示图像。
I want to display image in img control.
天冬氨酸code:
<asp:Image id="id" src='<%# Eval("Item.SmallPicPath") %>' Runat="Server" >
C#code:
C# code:
public void Page_Load(Object sender, EventArgs e)
{
Glasses item=GetItemByID(GlassesID) //retrive record from data base
}
眼镜是具有类属性SmallPicPath。
Glasses is a class that have property SmallPicPath.
我试图显示图像,但我得到错误的任何想法如何解决它?
I try to display image but i get error any idea how to fix it?
感谢你在前进!
推荐答案
您正在尝试使用的方法是基于数据绑定,这通常不利于单个图像。
The approach you're trying to use is based on databinding, which generally isn't good for a single image.
相反,尝试这样的标记:
Instead, try something like this markup:
<asp:Image id="MyImageId" runat="server">
然后,在你的code背后:
Then, in your code behind:
public void Page_Load(Object sender, EventArgs e)
{
Glasses item = GetItemByID(GlassesID); //retrive record from data base
this.MyImageId.ImageUrl = item.SmallPicPath;
}
如果你仍然想使用数据绑定出于某种原因,这样的事情应该工作(尽管相比其他方法的性能开销):
If you still want to use databinding for some reason, something like this should work (although there's a performance cost compared to the other approach):
<asp:Image id="MyImageId" runat="server" ImageUrl="<%# item.SmallPicPath %>">
code-背后:
Code-behind:
public Glasses item;
public void Page_Load(Object sender, EventArgs e)
{
this.item = GetItemByID(GlassesID); //retrive record from data base
this.MyImageId.DataBind();
}
数据绑定是可选操作;您需要调用的DataBind()
上的控制(或其母公司)来做到这一点。
Databinding is an optional operation; you need to call DataBind()
on the control (or its parent) to make it happen.
您只需要的eval(),在涉及数据容器的情况下(通常是通过数据源
) - 此一个没有
You only need "Eval()" in cases that involve a data container (usually via a DataSource
) -- which this one doesn't.
这篇关于如何在ASP的img控件中显示的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!