2012-02-17 63 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 
相关问题