user3218354
user3218354

Reputation: 1

Displaying a default images when the images is null in gridview using templatefield

I trying to display an alternate images when the images is null on my gridview which i used a templatefield to display the images. but i can only type a alternate text. There is no way to insert a alternate default images. Can u guys help me?

  <asp:TemplateField>
     <HeaderTemplate>
        Question Image
      </HeaderTemplate>
     <ItemTemplate>
      <img src='data:image/jpg;base64,<%#     Eval("QuestionImage") != System.DBNull.Value ? Convert.ToBase64String((byte[])Eval("QuestionImage")) : string.Empty %>'
                     alt="No image" height="100" width="200" />

    </ItemTemplate>
  </asp:TemplateField>

Upvotes: 0

Views: 1448

Answers (2)

Alice
Alice

Reputation: 1265

Could you please try this code for img tag?

<img src='<%# (Eval("QuestionImage") != System.DBNull.Value ? "data:image/jpg;base64," + Convert.ToBase64String((byte[])Eval("QuestionImage")) : "default.jpg") %>'
height="100" width="200" />

Where default.jpg is a filename for image you want to display when the image is null.

Upvotes: 0

JSHunjan
JSHunjan

Reputation: 397

One option can be at row databound event, you can go to that cell (template column) and provide the image source. If that is null, then change it to default image. For example:

Event: RowDatabound

Get value of cell (template column)

if(!string.IsNullOrEmpty(db.imagePath))
{
imgUser.src=db.imagePath;
}
else
{
imgUser.src="/Images/default.jpg";
}

This is an overall idea, not exact code, so please ignore any typing or code errors. Other thing is please avoid eval.

Upvotes: 1

Related Questions