Thursday, 23 February 2012

Image Showing in DataList

Showproduct.aspx
<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/MasterPage2.master" CodeFile="ShowProducts.aspx.cs" EnableEventValidation="false" Inherits="ShowProducts" %>

<script runat="server">

  
</script>

<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
 
        <div>
            <asp:DataList ID="DataList1" runat="server" RepeatColumns="3"
                RepeatDirection="Horizontal" ShowFooter="False" BackColor="White"
                BorderColor="Black" BorderStyle="Double" BorderWidth="5px" CellPadding="10"
                CellSpacing="20" Font-Bold="False" Font-Italic="False" Font-Overline="False"
                Font-Strikeout="False" Font-Underline="False" ForeColor="Black"
                GridLines="Both" HorizontalAlign="Center">
                <FooterStyle BackColor="White" ForeColor="#333333" />
                <ItemStyle BackColor="White" ForeColor="#333333" />
                <SeparatorStyle BackColor="#3333FF" Font-Bold="False" Font-Italic="False"
                    Font-Overline="False" Font-Strikeout="False" Font-Underline="False"
                    ForeColor="#CC00FF" HorizontalAlign="Center" VerticalAlign="Middle" />
                <SelectedItemStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
                <ItemTemplate>
                <table><tr>
                   
                <td><%#DataBinder.Eval(Container.DataItem,"images") %></td>
                <td>&nbsp;&nbsp;&nbsp</td>
                <td><h3><%#DataBinder.Eval(Container.DataItem,"productname") %></h3><br>
                <b>MRP</b> <%#DataBinder.Eval(Container.DataItem, "productmrp") %><br>
                <b>Our Price</b> <%#DataBinder.Eval(Container.DataItem, "plashprice") %><br>
                <b>Discount</b> <%#DataBinder.Eval(Container.DataItem, "Discount")%>%
                <br />
                <asp:Button ID="Button2" runat="server" Text="Get Details" CommandArgument='<%#Eval("productid") %>'
                        onclick="Button2_Click" />
                </td>
               
                </tr></table>
                 
                </ItemTemplate>
           
            </asp:DataList>
         
   
            <asp:Button ID="Button1" runat="server" Text="Button" />
   
    </div>
    </asp:Content>


//handler.ashx

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

using System;
using System.Web;
using System.Data.Odbc;

public class Handler : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {

        OdbcConnection con = new OdbcConnection(connection.connection1);
        con.Open();
        string imagegetquery = "select imagepath from tblproductimagemap where productid='" + context.Request.QueryString["productid"] + "'";
        OdbcCommand imagegetcommand = new OdbcCommand(imagegetquery, con);
        OdbcDataReader imagereader = imagegetcommand.ExecuteReader();
        imagereader.Read();
        context.Response.BinaryWrite((byte[])imagereader["imagepath"]);
        imagereader.Close();
        con.Close();
      
        //context.Response.ContentType = "text/plain";
        //context.Response.Write("Hello World");
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}


//showproduct.aspx.cs

  private void binddatalist()
    {
        DataTable dt= new DataTable();
        OdbcConnection con = new OdbcConnection(connection.connection1);
        con.Open();
        string imagegetquery = "select productid, productname, productbrand, productmrp, plashprice, discount from tblproductmaster";
        OdbcCommand imagegetcommand = new OdbcCommand(imagegetquery, con);
        OdbcDataAdapter imageadpter = new OdbcDataAdapter(imagegetcommand);
        imageadpter.Fill(dt);

        DataColumn imagecolumn = new DataColumn("images", typeof(string));
        dt.Columns.Add(imagecolumn);
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i][imagecolumn]=string.Format("<img src='Handler.ashx?productid={0}' alt='View Details' style='width:100px; height:100px'/>", dt.Rows[i][0].ToString());

            }
        }
       
       
        DataList1.DataSource = dt;
        DataList1.DataBind();

       

    }
   

No comments:

Post a Comment