How to Display image with Grideview
-
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
-
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
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 anItemTemplate
, 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
-
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
<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> -
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
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" %>'