Click here to Skip to main content
15,919,340 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
Hi,

I have 2 columns and 5 rows i need to insert the images in to the dynamically created gridview template field.


C#
public void PrepareGridView(ref GridView gvData, DataTable dataTable)
   {
       try
       {
           int count = 0;
           foreach (DataColumn column in dataTable.Columns)
           {
               if (column.DataType == typeof(System.Byte[]))
               {
                   TemplateField bfield = new TemplateField();
                   bfield.HeaderText = column.ColumnName;
                   Image image = new Image();
                   image.ID = imageId;
                   image.Height = 80;
                   image.Width = 80;
                   ItemTemplate itemTemplate = new ItemTemplate(ListItemType.Item, image);
                   bfield.ItemTemplate = itemTemplate;
                   gvData.Columns.Add(bfield);
                   count++;
               }
           }
       }
       catch (Exception ex)
       {
       }
   }


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

public class ItemTemplate : ITemplate
{
    ListItemType itemType;
    Image imageItem;
    public ItemTemplate(ListItemType type, Image image)
    {
        itemType = type;
        imageItem = image;
    }
    void ITemplate.InstantiateIn(System.Web.UI.Control container)
    {
        switch (itemType)
        {
            case ListItemType.Item:
                container.Controls.Add(imageItem);
                break;
        }
    }
}


Thanks,
Posted
Comments
F-ES Sitecore 23-Jul-15 7:25am    
You can't embed binary data on the page like that, you need to add <img> tags, or an asp:Image control on the page that point to a valid url where the image can be downloaded. If your images are in a database then you'll need to implement a handler to allow the browser to download the image. Or you can use base64 encoding in an img tag if you want to embed the image data on the page itself.

Google "asp.net handler download image database" for code samples on how you can provide an image handler.
Richard Deeming 23-Jul-15 8:56am    
Small images could be embedded using a data URI[^].
F-ES Sitecore 23-Jul-15 10:07am    
That's what I meant when I refered to base64 encoding, I didn't want to elaborate much as it is a poor solution, even for small images.
Did you try giving some url to the ImageUrl property?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900