Link to home
Start Free TrialLog in
Avatar of Mikerhonda
Mikerhonda

asked on

Open a new window using Page.Response

Hi.  I'm using the following code to view a picture stored as an image data type in a database:

With Page.Response
            .Clear()
            .ContentType = Type
            If DownloadFileName <> "" Then
                Page.Response.AddHeader("content-disposition", "filename=" & DownloadFileName)
            End If
            .OutputStream.Write(Data, 0, Length)
            .End()
End With

This works however my problem is that I want to open this view in a new window.  I know that using a hyperlink or linkbutton I can set target = _blank to open a new window but how can I do that with page.response?

Thanks
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Mikerhonda,
Try this:

Response.write("<script>window.open('Yourpage.aspx')</script>")
Hi Mikerhonda,

You cannot force a client browser to start a new window purely from the server-side. As you say, it can be done from a link or button (or with javascript from almost anything) and that is the only way to do so. Believe me, you don't really want it any other way. Allowing a server to spawn new windows whenever it feels like it would make your internet experience quite unpleasant, as then even popup-blockers would have a hard time controlling the thousands of windows that would get opened by those with nothing better to do than annoy us!

There are of course ways to get close, the simplest if you absolutely have to have a new window is to write some javascript that can be sent to the client to force this, however it is far better to handle this client-side on the original trigger event that causes this image to be displayed as you will then end up with a new window with your image and quite probably a blank page in your originating window.

Tim Cottee
Avatar of Mikerhonda
Mikerhonda

ASKER

Tim:
Can I use something other than Page.Response.OutputStream.Write to do this then?  I'd love to do this with a hyperlink but the problem is that there is really no url to go to.

Mike
Mikerhonda,

Well that depends on how you have architected this, if the image display is a seperate file then it is easy to use a hyperlink to do this. If however as I guess you have done here then you have this in the code-behind for the page itself. That is why you have a problem in doing what you are trying to do.

Perhaps if you can explain the process and your desired result we can come up with a workable solution.

Tim
K.  What I'm doing is using a linkbutton to pull the data for the image from the database:

Private Sub editVisualAidsPath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles editVisualAidsPath.Click
        Try
            dr = SqlHelper.ExecuteReader(oConn, CommandType.StoredProcedure, "SP_QualityAlertSelect", New SqlClient.SqlParameter("@QAID", QAID))
            If dr.HasRows Then
                dr.Read()
                Dim bytFile As Array = dr("VisualAidsFile")
                Dim strType As String = dr("Type")
                Dim intLength As Integer = dr("Length")
                Dim DownloadFileName As String = dr("VisualAidsPath")
                dr.Close()
                DeliverFile(Me, bytFile, strType, intLength, DownloadFileName)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
            dr.Close()
        End Try
End Sub

Here's the 'DeliverFile' sub:
Public Sub DeliverFile(ByVal Page As System.Web.UI.Page, ByVal Data() As Byte, ByVal Type As String, ByVal Length As Integer, Optional ByVal DownloadFileName As String = "")
        With Page.Response
            .Clear()
            .ContentType = Type
            If DownloadFileName <> "" Then
                Page.Response.AddHeader("content-disposition", "filename=" & DownloadFileName)
            End If
            .OutputStream.Write(Data, 0, Length)
            .End()
        End With
End Sub

So the image data is stored in a database and when the user clicks on the linkbutton I want the image to be displayed in a separate window.  What I would REALLY love is a control for ASP similar to the PictureBox control on windows forms that will render an image directly from image data stored in a database.

Mike
Mikerhonda,

Ok, move the code into a new page called ShowImage.aspx like this and get the QAID value from the querystring:

Private Sub Page_Load .....
        Try
            dr = SqlHelper.ExecuteReader(oConn, CommandType.StoredProcedure, "SP_QualityAlertSelect", New SqlClient.SqlParameter("@QAID", Request.QueryString("QAID")))
            If dr.HasRows Then
                dr.Read()
                Dim bytFile As Array = dr("VisualAidsFile")
                Dim strType As String = dr("Type")
                Dim intLength As Integer = dr("Length")
                Dim DownloadFileName As String = dr("VisualAidsPath")
                dr.Close()
                DeliverFile(Me, bytFile, strType, intLength, DownloadFileName)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
            dr.Close()
        End Try
End Sub

Public Sub DeliverFile(ByVal Page As System.Web.UI.Page, ByVal Data() As Byte, ByVal Type As String, ByVal Length As Integer, Optional ByVal DownloadFileName As String = "")
        With Page.Response
            .Clear()
            .ContentType = Type
            If DownloadFileName <> "" Then
                Page.Response.AddHeader("content-disposition", "filename=" & Request.QueryString("FileName"))
            End If
            .OutputStream.Write(Data, 0, Length)
            .End()
        End With
End Sub

Now whenever you call ShowImage.aspx?QAID=1234 for example you will get that image returned to you.

So what this means is that you can use it client-side providing you know the value of QAID.

For example, if QAID is entered into a textbox on the form, you can simply do this:


editVisualAidsPath.Attributes.Add("OnClick","document.getElementById('ImageElementToDisplayPicture').src='/showimage.aspx?qaid='+document.getElementById('TextFieldWithQAIDIn').value;return false;")

What this does is takes the value from the TextFieldWithQAIDIn object and sends it to the showimage page. This is then assigned as the image source for the other object.

Tim
Mikerhonda ,
>>..What I would REALLY love is a control for ASP similar to the PictureBox control on windows forms that will render an image directly from image data stored in a database.
Based on my understanding of your exact senario, you can try this solution :

if not ispostback then
    dr = SqlHelper.ExecuteReader(oConn, CommandType.StoredProcedure, "SP_QualityAlertSelect", New    SqlClient.SqlParameter("@QAID", QAID))
            If dr.HasRows Then
                dr.Read()
                Dim DownloadFileName As String = dr("VisualAidsPath")
                dr.Close()

     editVisualAidsPath.attributes.add("onClick","javascript:window.open('" & DownloadFileName & "');return false;")
end if
Sorry, TimCottee . Didn't see your message. Hmm, have to clean browser's cache.. Let you continue the solution here. Have a nice day to all of you guys. Bye
Tim:
What is 'ImageElementToDisplayPicture'?
Mikerhonda,

Me typing quicker than I was thinking!

Actually you need:

editVisualAidsPath.Attributes.Add("OnClick","window.open('/showimage.aspx?qaid='+document.getElementById('TextFieldWithQAIDIn').value,'','toolbar=no,status=no,menubar=no,height=200,width=200,resizable=no,location=no');return false;")

Which will open a popup window 200x200 in size without the toolbar, menu etc and just showing your image.

Tim
OK.  We're getting closer....

The code you wrote WITH the text box identifier:

Me.editVisualAidsPath.Attributes.Add("OnClick", "window.open('/showimage.aspx?qaid='+document.getElementById(me.editQualityAlertNumber.text).value,'','toolbar=no,status=no,menubar=no,height=200,width=200,resizable=no,location=no');return false;")

This is not opening the ShowImage.aspx page.  I have a breakpoint at the page load and it isn't even making it to that.  Could it be due to how my field identifier is coded (me.editQualityAlertNumber.text)?

Thanks so much for your time and help

Mike
ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial