0

I created this code with the intention that, when clicked on, this ImageButton will redirect the PDF to a new tab/new window. Unfortunately it does not work. When I clicked on the ImageButton it passed to current tab/window. Please help.

<asp:ImageButton ID="ImageButton14" runat="server" Height="16px" 
                        ImageUrl='image/viewIcon.png' target="_blank" 
                        CommandName="openpdf" CommandArgument='<%# Bind("modul_filePath") %>'/>

Code behind:

If e.CommandName = "openpdf" Then
    Dim modul_filePath As String = e.CommandArgument.ToString()
    Using myConnection
        myConnection = New SqlConnection(xxxxxxx)
        ' ...
    End Using
    strPath = modul_filePath
    Response.ContentType = "application/pdf"
    Response.Redirect("" + strPath.Trim)
    Response.End()
End If
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
airina
  • 3
  • 5
  • Hi airina, could you check on the generated html? and do remember, [`CommandName` and `CommandArgument` pair](https://stackoverflow.com/questions/4050478/what-is-the-purpose-of-command-name-and-command-argument-for-a-control-example-b) may introduce javascript that fires a [postback](https://stackoverflow.com/questions/4251157/what-is-a-postback) instead of following a hyperlink normally. – Bagus Tesa Sep 27 '19 at 06:27
  • Hi Bagus Tesa, I already check the vb code but does no fire a postback method – airina Sep 27 '19 at 06:32
  • Hi Airina, take a look here: https://stackoverflow.com/questions/34510275/how-to-open-page-in-new-tab-using-the-response-redirect-at-asp-net – Egbert Sep 27 '19 at 06:43
  • @AndrewMorton Excellent!! It works for me. Really appreciate your idea. Thank you – airina Oct 02 '19 at 08:35

1 Answers1

0

Rather than persuading an asp:ImageButton to do the task, you could style an ordinary <a> element to use an image, or put an <img> inside the <a>. There are some more ideas at Hyperlinking an image using CSS.

Going with my second suggestion, the markup would be along the lines of:

<a href="yourHandlerToServePdf.ashx?file=<%# Bind("modul_filePath") %>" target="_blank">
    <img src="image/viewIcon.png">
</a>

For an explanation of the handler part, A summary of generic handlers: What is a Generic Handler in asp.net and its use?

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84