Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1253
  • Last Modified:

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
0
Mikerhonda
Asked:
Mikerhonda
  • 5
  • 4
  • 3
1 Solution
 
David H.H.LeeCommented:
Mikerhonda,
Try this:

Response.write("<script>window.open('Yourpage.aspx')</script>")
0
 
TimCotteeCommented:
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
0
 
MikerhondaAuthor Commented:
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TimCotteeCommented:
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
0
 
MikerhondaAuthor Commented:
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
0
 
TimCotteeCommented:
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
0
 
David H.H.LeeCommented:
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
0
 
David H.H.LeeCommented:
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
0
 
MikerhondaAuthor Commented:
Tim:
What is 'ImageElementToDisplayPicture'?
0
 
TimCotteeCommented:
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
0
 
MikerhondaAuthor Commented:
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
0
 
TimCotteeCommented:
Mikerhonda,

The attributes.add will add this as a client-side event so your breakpoint will not fire as this doesn't happen on the server-side at all.

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

Might be better, this translates asp.net's uniqueID value into the ID value used by clientside javascript so that it should work. Of course if you have a popup blocker active this also may give you a problem in displaying this popup.
Tim
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now