2013-05-06 91 views
0

我有一個問題,我的圖片來自客戶簽名的數據庫。在formview中顯示圖像

我怎樣才能綁定,使用<% #Eval() %>

  <tr> 
       <td> 
        Display Signature : 
       </td> 
       <td> 
        <%#Eval("SignatureImage") %> 
       </td> 
      </tr> 
+0

在我看來,圖片必須包含在''標籤中。您的「SignatureImage」屬性是否發出圖片標籤? – 2013-05-06 12:08:18

+0

簽名是什麼格式。如果它的二進制文件,你可以保存它,要麼將其轉換爲base64內聯圖像,要麼使用不同的處理程序來顯示它 – Aristos 2013-05-06 12:09:54

回答

1

Formview簽名可以使用的HttpHandler檢索二值圖像,並與SRC屬性,然後將其綁定到一個圖像contorl。檢查這個完整的示例代碼:

ImgHandler.ashx:

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

using System; 
using System.Web; 
using System.Data.Sql; 
using System.Data.SqlClient; 


public class ImgHandler : IHttpHandler 
{ 

    public void ProcessRequest (HttpContext context) 
    { 
     byte[] buffer = null; 
     string querySqlStr = ""; 
     if (context.Request.QueryString["ImageID"] != null) 
     { 
      querySqlStr="select * from testImageTable where ID="+context.Request.QueryString["ImageID"]; 
     } 
     else 
     { 
      querySqlStr="select * from testImageTable"; 
     } 
     SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["TestDataBaseConnectionString"].ToString()); 
     SqlCommand command = new SqlCommand(querySqlStr, connection); 
     SqlDataReader reader = null; 
     try 
     { 
      connection.Open(); 
      reader = command.ExecuteReader(); 
      //get the extension name of image 
      while (reader.Read()) 
      { 
       string name = reader["imageName"].ToString(); 
       int endIndex = name.LastIndexOf('.'); 
       string extensionName = name.Remove(0, endIndex + 1); 
       buffer = (byte[])reader["imageContent"]; 
       context.Response.Clear(); 
       context.Response.ContentType = "image/" + extensionName; 
       context.Response.BinaryWrite(buffer); 
       context.Response.Flush(); 
       context.Response.Close(); 
      } 
      reader.Close(); 

     } 
     finally 
     { 
      connection.Close(); 
     } 
    } 

    public bool IsReusable 
    { 
     get 
     { 
      return false; 
     } 
    } 

} 

aspx文件:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 

     <img src="ImgHandler.ashx?ImageID=1" alt="" /> 
     <img src="ImgHandler.ashx?ImageID=2" alt="" /> 
     <img src="ImgHandler.ashx?ImageID=3" alt="" /> 
     <img src="ImgHandler.ashx?ImageID=4" alt="" /> 

    </div> 
    </form> 
</body> 
</html> 

如果不這樣做的HTTP處理程序,請檢查下面的參考資料:

http://msdn.microsoft.com/en-us/library/bb398986.aspx

https://stackoverflow.com/questions/1096122/how-do-i-use-an-ashx-handler-with-an-aspimage- object