如何在数据列表中显示默认图像 [英] How to show default image in datalist
本文介绍了如何在数据列表中显示默认图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有一个数据列表,其中有一个Image控件,并且在Image Control中,我正在从数据库中获取数据,但是如果表中image字段中没有值,那么我想在Image控件中显示默认图像. >
我做了以下事情:
Hi All,
I have a datalist in that I have a Image control and in Image Control I am fetching data from database but thing is this if there is no value in image field in table then I want to show a default image in Image control .
I did something like below:
<asp:Image ID="Image1" Width="75%" Height="75%" runat="server" ImageUrl=''<%# Eval("Photo") %>'' />
请帮帮我.
在此先感谢.
问候,
Mohd Wasif
Please help me.
Thanks in Advance.
Regards,
Mohd Wasif
推荐答案
您可以尝试执行以下操作:
You could try something like this:
<asp:Image ID="Image1" Width="75%" Height="75%" runat="server" ImageUrl=''<%# (Eval("Photo") ?? "path/to/default/image.jpg") %>'' />
希望对您有所帮助.
Hope it helps.
您可以将其用作
You can use it as
<asp:Image ID="Image1" Width="75%" Height="75%" runat="server" ImageUrl=''<%# SetPhoto(Eval("Photo")) %>'' />
public string SetPhoto(string actualUrl)
{
if(actualUrl != "")
return actualUrl;
else
return @"~/Images/defaul.png";
}
找到要显示的默认图像,将其放置在您的网站目录下,假设您的默认图像名称为default.jpg
,现在在您的页面下方使用此代码
find a default image you want to show, place it under your web site roor, lets say your default image name isdefault.jpg
, now use this code below in your page
<asp:Image ID="Image1" Width="75%" Height="75%" runat="server" ImageUrl=''<%# (String.IsNullOrEmpty(Eval("Photo").ToString()) ? Eval("Photo"):"~/default.jpg") %>'' />
就是这样.
Thats it..
这篇关于如何在数据列表中显示默认图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文