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("c ontent-dis position", "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
With Page.Response
.Clear()
.ContentType = Type
If DownloadFileName <> "" Then
Page.Response.AddHeader("c
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
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
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
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
Can I use something other than Page.Response.OutputStream
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
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
ASKER
K. What I'm doing is using a linkbutton to pull the data for the image from the database:
Private Sub editVisualAidsPath_Click(B yVal sender As System.Object, ByVal e As System.EventArgs) Handles editVisualAidsPath.Click
Try
dr = SqlHelper.ExecuteReader(oC onn, CommandType.StoredProcedur e, "SP_QualityAlertSelect", New SqlClient.SqlParameter("@Q AID", 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("c ontent-dis position", "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
Private Sub editVisualAidsPath_Click(B
Try
dr = SqlHelper.ExecuteReader(oC
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("c
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(oC onn, CommandType.StoredProcedur e, "SP_QualityAlertSelect", New SqlClient.SqlParameter("@Q AID", 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("c ontent-dis position", "filename=" & Request.QueryString("FileN ame"))
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.Attribu tes.Add("O nClick","d ocument.ge tElementBy Id('ImageE lementToDi splayPictu re').src=' /showimage .aspx?qaid ='+documen t.getEleme ntById('Te xtFieldWit hQAIDIn'). value;retu rn 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
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(oC
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("c
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.Attribu
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(oC onn, CommandType.StoredProcedur e, "SP_QualityAlertSelect", New SqlClient.SqlParameter("@Q AID", QAID))
If dr.HasRows Then
dr.Read()
Dim DownloadFileName As String = dr("VisualAidsPath")
dr.Close()
editVisualAidsPath.attribu tes.add("o nClick","j avascript: window.ope n('" & DownloadFileName & "');return false;")
end if
>>..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(oC
If dr.HasRows Then
dr.Read()
Dim DownloadFileName As String = dr("VisualAidsPath")
dr.Close()
editVisualAidsPath.attribu
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
ASKER
Tim:
What is 'ImageElementToDisplayPict ure'?
What is 'ImageElementToDisplayPict
Mikerhonda,
Me typing quicker than I was thinking!
Actually you need:
editVisualAidsPath.Attribu tes.Add("O nClick","w indow.open ('/showima ge.aspx?qa id='+docum ent.getEle mentById(' TextFieldW ithQAIDIn' ).value,'' ,'toolbar= no,status= no,menubar =no,height =200,width =200,resiz able=no,lo cation=no' );return false;")
Which will open a popup window 200x200 in size without the toolbar, menu etc and just showing your image.
Tim
Me typing quicker than I was thinking!
Actually you need:
editVisualAidsPath.Attribu
Which will open a popup window 200x200 in size without the toolbar, menu etc and just showing your image.
Tim
ASKER
OK. We're getting closer....
The code you wrote WITH the text box identifier:
Me.editVisualAidsPath.Attr ibutes.Add ("OnClick" , "window.open('/showimage.a spx?qaid=' +document. getElement ById(me.ed itQualityA lertNumber .text).val ue,'','too lbar=no,st atus=no,me nubar=no,h eight=200, width=200, resizable= no,locatio n=no');ret urn 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
The code you wrote WITH the text box identifier:
Me.editVisualAidsPath.Attr
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
Thanks so much for your time and help
Mike
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try this:
Response.write("<script>wi