Reputation: 867
i have an image
control whose source can be updated at run time because i am showing employee's picture in it and this was loaded from database as:
EmpImage.Src = "data:image/png;base64," + base64String;
and to update record i need to check image source is empty or not as:
if (EmpImage.Src.Length == 0)
or
if (EmpImage.Src == "")
but it shows me empty all the time...
i read this thread ASP.NET image src question but here jquery
is used but I need server side solution
EDIT:
i read image from database to image control as:
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;
}
where dt
is datatable
and i got this from google
i need your help to get out of it.
i am using asp.net c#
thanks in advance.
Upvotes: 1
Views: 2262
Reputation: 207
instead of Src use ImageUrl property
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;
}
Upvotes: 1
Reputation: 8271
Set your image object to the following presentation:
Aspx
<asp:Image id="test" runat="server" ImageUrl='<%# GetImage(Eval("Pic")) %>' />
Cs
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 ;
}
Upvotes: 0
Reputation: 21795
Server side Image control has ImageUrl property which is of String
type, so you can directly use string functions:-
if (String.IsNullOrEmpty(EmpImage.ImageUrl)
{
}
Update:
As per the exception posted by you, it is sure that you are not using the ASP.NET server side Image
control i.e. <asp:Image
but the html image control with runat="server"
tag. So in this case agian since Src property is of type String
, you can use the same method as:-
if (String.IsNullOrEmpty(EmpImage.Src)
{
}
Upvotes: 2
Reputation: 780
As far as i know, whenever you set image source dynamically, you will never get source length/info on page posting. You have to manage some flag using hidden inputs.
Upvotes: 0