的形象.SRC在asp.net中总是空的
本文关键字:net asp SRC | 更新日期: 2023-09-27 18:16:12
我有一个image
控件,其源可以在运行时更新,因为我在其中显示员工的图片,这是从数据库加载的:
EmpImage.Src = "data:image/png;base64," + base64String;
和更新记录,我需要检查图像源是否为空:
if (EmpImage.Src.Length == 0)
或
if (EmpImage.Src == "")
但是它总是显示空的…
我读这个线程ASP。. NET image src问题但这里使用的是jquery
但我需要服务器端解决方案
i read image from database to image control:
if (dt.Rows[0]["Pic"] != DBNull.Value)
{
byte[] bytes = (byte[])dt.Rows[0]["Pic"];
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
// EmpImage.ImageUrl = base64String;
EmpImage.Src = "data:image/png;base64," + base64String;
}
其中dt
是datatable
,我从谷歌得到这个
我需要你的帮助才能摆脱困境。
我使用asp.net c#
服务器端图像控件有ImageUrl属性,它是String
类型,所以你可以直接使用字符串函数:-
if (String.IsNullOrEmpty(EmpImage.ImageUrl)
{
}
更新:
根据您发布的例外,可以肯定您没有使用ASP。. NET服务器端Image
控件即<asp:Image
,而html图像控件带有runat="server"
标签。所以在这种情况下,因为Src属性的类型是String
,你可以使用相同的方法:-
if (String.IsNullOrEmpty(EmpImage.Src)
{
}
代替Src使用ImageUrl属性
if (dt.Rows[0]["Pic"] != DBNull.Value)
{
byte[] bytes = (byte[])dt.Rows[0]["Pic"];
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
// EmpImage.ImageUrl = base64String;
EmpImage.ImageUrl = "data:image/png;base64," + base64String;
}
据我所知,无论何时动态设置图像源,您都不会在页面发布上获得源长度/信息。您必须使用隐藏输入来管理一些标志
设置您的图像对象为以下表示:
Aspx
<asp:Image id="test" runat="server" ImageUrl='<%# GetImage(Eval("Pic")) %>' />
c
public string GetImage(object img)
{
string base64String ="";
if (dt.Rows[0]["Pic"] != DBNull.Value)
{
byte[] bytes = (byte[])dt.Rows[0]["Pic"];
base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
}
return "data:image/png;base64," + base64String ;
}