Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to Display image with Grideview

How to Display image with Grideview

Scheduled Pinned Locked Moved ASP.NET
helptutorialcsharpasp-netcom
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    langpolen
    wrote on last edited by
    #1

    I have a problem with using grideview to display data I don't want to use Default display in gridview. For example ------------------------- Image1 Title="###" | Date="###" | Price="###" | -------------------------| Please all friends help me to solve this in asp.net Please kindly to email to me by polen.lang@gmail.com

    S A B 3 Replies Last reply
    0
    • L langpolen

      I have a problem with using grideview to display data I don't want to use Default display in gridview. For example ------------------------- Image1 Title="###" | Date="###" | Price="###" | -------------------------| Please all friends help me to solve this in asp.net Please kindly to email to me by polen.lang@gmail.com

      S Offline
      S Offline
      Sandeep Mewara
      wrote on last edited by
      #2

      You need to use Template Fields. A TemplateField object enables you to specify templates that contain markup and controls to customize the layout and behavior of a column in a GridView control. Using an ItemTemplate, you can specify the layout to be used when the GridView displays data in a column. Read all about it here: MSDN: Using TemplateFields in the GridView Control[^] MSDN: TemplateField.ItemTemplate Property[^]

      Sandeep Mewara Microsoft ASP.NET MVP 2012 & 2013 [My Blog]: Sandeep Mewara's Tech Journal! [My Latest Article]: HTML5 Quick Start Web Application

      1 Reply Last reply
      0
      • L langpolen

        I have a problem with using grideview to display data I don't want to use Default display in gridview. For example ------------------------- Image1 Title="###" | Date="###" | Price="###" | -------------------------| Please all friends help me to solve this in asp.net Please kindly to email to me by polen.lang@gmail.com

        A Offline
        A Offline
        AANIL DHAKAD
        wrote on last edited by
        #3

        <asp:GridView ID="GridView1" runat="server">
        <Columns>
        asp:TemplateField
        <ItemTemplate>
        <asp:Image ID="img1" runat="server" ImageUrl='<%#Eval("imageUrlFielName") %>' />
        </ItemTemplate>
        </asp:TemplateField>
        </Columns>
        </asp:GridView>

        1 Reply Last reply
        0
        • L langpolen

          I have a problem with using grideview to display data I don't want to use Default display in gridview. For example ------------------------- Image1 Title="###" | Date="###" | Price="###" | -------------------------| Please all friends help me to solve this in asp.net Please kindly to email to me by polen.lang@gmail.com

          B Offline
          B Offline
          Bikash Prakash Dash
          wrote on last edited by
          #4

          create a generic handler file e.g.-> showpic.ashx with code

          <%@ WebHandler Language="C#" Class="showpic" %>

          using System;

          using System.Web;

          using System.Data.SqlClient;

          using System.Data;

          using System.IO;

          using System.Collections.Specialized;

          public class showpic: IHttpHandler {

          public string GetConnectionString()
          
          {
          
              //sets the connection string from your web config file "ConnString" is the name of your Connection String
          
              return System.Configuration.ConfigurationManager.ConnectionStrings\["b4mConnectionString"\].ConnectionString;
          
          }
          
          public void ProcessRequest(HttpContext context)
          
          {
          
              string id = context.Request.QueryString\["id"\]; //get the querystring value that was pass on the ImageURL (see GridView MarkUp in Page1.aspx)
              string img = context.Request.QueryString\["img"\];
              if (id != null)
          
              {
          
          
                  try
                  {
                      MemoryStream memoryStream = new MemoryStream();
          
                      SqlConnection connection = new SqlConnection(GetConnectionString());
          
                      string sql = "SELECT " + img + " FROM useraccount WHERE userid= @id";
          
          
          
                      SqlCommand cmd = new SqlCommand(sql, connection);
          
          
                      cmd.Parameters.AddWithValue("@id", id);
          
                      connection.Open();
          
          
          
                      SqlDataReader reader = cmd.ExecuteReader();
          
                      reader.Read();
          
          
                      byte\[\] file = (byte\[\])reader\[0\];
          
          
          
                      reader.Close();
          
                      connection.Close();
          
                      memoryStream.Write(file, 0, file.Length);
          
                      context.Response.Buffer = true;
          
                      context.Response.BinaryWrite(file);
          
                      memoryStream.Dispose();
                  }
                  catch
                  { 
                  
                  }
          
          
              }
          
          }
          
          
          
          public bool IsReusable {
          
              get {
          
                  return false;
          
              }
          
          }
          

          }

          change the database & query parameters within the gridview create a templated field with Image control In Image Url property set the following code

          '<%# "generics/showpic.ashx?id="+Eval("userid")+"&img=picture" %>'

          e.g-> <asp:Image ID="Image1" runat="server" Height="60px" ImageUrl='<%# "generics/showpic.ashx?id="+Eval("userid")+"&img=picture" %>'

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups