2012-02-17 73 views
0

在我的頁面中,我試圖顯示兩個具有相同ID的圖像。爲此,我有兩個圖像控件(imgX,imgY)。要將圖像寫入圖像控件,我使用HttpHandler(ashx)。我們是否需要爲每個圖像創建一個HttpHandler(ashx)?

我的問題是,我得到了相同的圖像追加到兩個控件(IMGX,IMGY)

在頁面加載事件,這是我的代碼:

imgPhoto.ImageUrl = "Image.ashx?EmpBadge=" & Session("EmpBadge") 
imgSign.ImageUrl = "Image.ashx?EmpBadge=" & Session("EmpBadge") 

而且在ASHX:

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest 
     Try 
      Dim imageId As String = context.Request.QueryString("EmpBadge") 
      Dim drPhoto As SqlDataReader 
      Dim param(1) As SqlParameter 
      param(1) = New SqlParameter("@EmpBadge", imageId) 
      drPhoto = IMSIndia.ExecuteReaderWithParam("SpGetPhotoAndSignature", param) 
      If drPhoto.HasRows Then 
       While drPhoto.Read() 
        context.Response.ContentType = "image/" & drPhoto("PhotoType") 
        context.Response.BinaryWrite(DirectCast(drPhoto("Photo"), Byte())) 
        context.Response.ContentType = "image/" & drPhoto("Signaturetype") 
        context.Response.BinaryWrite(DirectCast(drPhoto("Signature"), Byte())) 
       End While 
      End If 
     Catch ex As Exception 

     Finally 
      If IMSIndia.con.State = ConnectionState.Open Then 
       IMSIndia.ConnectionClose() 
      End If 
     End Try 
    End Sub 

謝謝。

+0

爲什麼不傳遞像''imagetype = photo「'這樣的另一個查詢字符串,並且使用簡單的if-else方法來控制ProcessRequest的控制流? – naveen 2012-02-17 05:38:02

回答

1

嗯...... 當然是你會得到相同的圖像爲每個;您傳遞的每個網址都完全相同。您正在爲兩者使用相同的Session值。

然後,在您的代碼中,您似乎試圖在相同的響應中發送兩個圖像。這是毫無意義的,至少我不確定它是否與這個問題有關。

您需要根據QueryString值區分圖像。除非你這樣做,否則你的處理程序不能分辨出區別

+0

請參閱他正在使用他的數據列中的不同列。 – naveen 2012-02-17 05:48:35

1

你應該像這樣改變你的代碼。

在Page_Load中

imgPhoto.ImageUrl = "Image.ashx?ImageType=photo&EmpBadge=" & Session("EmpBadge") 
imgSign.ImageUrl = "Image.ashx?ImageType=signature&EmpBadge=" & Session("EmpBadge") 

裏面的while環路ProcessRequest內,代替的if-else這樣。

If drPhoto.HasRows Then 
    While drPhoto.Read() 
     If context.Request.QueryString("ImageType") = "photo" Then 
      context.Response.ContentType = "image/" & drPhoto("PhotoType") 
      context.Response.BinaryWrite(DirectCast(drPhoto("Photo"), Byte())) 
     Else 
      context.Response.ContentType = "image/" & drPhoto("Signaturetype") 
      context.Response.BinaryWrite(DirectCast(drPhoto("Signature"), Byte())) 
     End If 
    End While 
End If 
相關問題