使用linq在转发器中查看图像 [英] view image in repeater using linq
问题描述
能帮我在使用LINQ绑定到IEnumerable
对象的中继器中显示图像吗?
我想在网页中正确查看图像.
Can you please help me to show an image inside a repeater which is bound to an IEnumerable
object using LINQ.
I want to view the image properly in the web page.
推荐答案
在
repeater
中查看图像不相关LINQ中的任何内容.
View image in
repeater
does not relates anything in LINQ.
使用Linq创建IEnumerable
List,因此只需使用
Using Linq you create IEnumerable
List, so just bind it to the Repeater control using
Repeater.DataSource = IEnumerableObject;
Repeater.DataBind();
将其绑定到Repeater控件即可查看图像,您需要存储图像到有效位置(可能是一个临时文件夹)/您可以直接在Response Stream中写入.
在ItemDataBound
eventhandler中执行此操作.
Now to view image you need to store the image into a valid location (might be a temporary folder) / you can directly write in Response Stream.
Do this in ItemDataBound
eventhandler.
protected void rptImages_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem ||
e.Item.ItemType == ListItemType.Item)
{
string imagename = yourimagename;
HtmlImage imgImage = e.Item.FindControl("img") as HtmlImage;
string imagepath = Path.Combine(Path.Combine(Request.PhysicalApplicationPath,
"tempImages"), imagename);
if (!File.Exists(imagepath))
File.WriteAllBytes(imagepath, image.Image);
imgImage.Src = "~/tempImages/" + imagename;
imgImage.Attributes["title"] = imagename;
}
}
请记住使用特定图像所需的文件名来修改yourimagename
.
我已经使用临时文件夹显示了此内容,您可以使用单独的处理程序轻松对其进行修改,并在响应流中编写整个filebytes
.
Remember to modify yourimagename
with the filename you want for the particular image.
I have shown this using temporary folder, you can easily modify this using a separate handler and write the entire filebytes
in response stream.
这篇关于使用linq在转发器中查看图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!