0

尝试在基于employeeid的图像控件中从数据库中检索和显示图像...我采用了一个httphandler,其中包含以下内容:

  Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

    'context.Response.ContentType = "text/plain"
    'context.Response.Write("Hello World!")

    Dim employeeId As Integer
    If (Not (context.Request.QueryString("employeeId")) Is Nothing) Then
        employeeId = Convert.ToInt32(context.Request.QueryString("employeeId"))
    Else
        Throw New ArgumentException("No parameter specified")
    End If
    Dim imageData() As Byte = {}
    ' get the image data from the database using the employeeId Querystring
    context.Response.ContentType = "image/jpeg"
    ' You can retrieve this also from the database
    context.Response.BinaryWrite(imageData)

End Sub

Protected Sub DisplayButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles DisplayButton.Click
    bind()
    GridView1.Visible = "True"
    ProcessRequest(Context)
End Sub

错误:The 'MasterPageFile' property can only be set in or before the 'Page_PreInit' event. 我哪里出错了?我需要进行哪些更改?

这是窗体上的图像控件:

<asp:Image ID="Image1" runat="server" imageUrl="HttpHandler.ashx?employeeId=5"/>

@Stefano Altieri:

这是在 Employee.aspx 上

Protected Sub DisplayButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles DisplayButton.Click
    bind()
    GridView1.Visible = "True"
    Image1.ImageUrl = "~/HttpHandler.ashx?EmployeeID='" & EmailIDTextBox.Text & "'"
End Sub

这是在 HttpHandler.ashx

Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

    'context.Response.ContentType = "text/plain"
    'context.Response.Write("Hello World!")

    Dim employeeId As Integer
    If (Not (context.Request.QueryString("employeeId")) Is Nothing) Then
        employeeId = Convert.ToInt32(context.Request.QueryString("employeeId"))
    Else
        Throw New ArgumentException("No parameter specified")
    End If
    Dim imageData() As Byte = {}
    ' get the image data from the database using the employeeId Querystring
    context.Response.ContentType = "image/jpeg"
    ' You can retrieve this also from the database
    context.Response.BinaryWrite(imageData)

End Sub
4

2 回答 2

0

你的处理程序应该是这样的

Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim employeeId As Integer
If (Not (context.Request.QueryString("employeeId")) Is Nothing) Then
employeeId = Convert.ToInt32(context.Request.QueryString("employeeId"))
Dim con As SqlConnection("your connection string")    
Dim cmd As SqlCommand("select image_colum from <table_name> where employeeID = "+employeeID)
con.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
dr.Read()
Dim picture As Byte() = dr[0]
context.Response.ContentType = "image/jpeg"
context.Response.BinaryWrite(picture)
End If
End Sub
于 2013-03-04T10:19:57.523 回答
0

通用处理程序可以帮助您..

于 2013-03-04T09:45:05.037 回答