Link to display attachment

Asked

Viewed 270 times

1

I have a hyperlink to view my attachment. My attachment is on the server and your path is saved in the database. I’m wanting at the time that the person click on Hyerlink open a new tab and present the attachment.

Follows my code

function popuplista(c) {
    popupwide("'"+ c +"'");
}

<asp:HyperLink ID="lblMensagemAnexo" runat="server" NavigateUrl='<%# Eval("Anexo") %>'
                        Text="Clique aqui para ver o anexo" Width="250px" Visible="false" Target="_blank"></asp:HyperLink>

lblMensagemAnexo.NavigateUrl = "javascript:popuplista('" + dt.Rows[0]["Anexo"].ToString() + "');";

2 answers

1

A while back I was having that problem too.

After searching, I couldn’t find a way to do this through ASP.NET.

The method I was able to do to solve the problem was to add an event to the LinkButton:

<asp:LinkButton ID="lblMensagemAnexo" runat="server" CommandArgument='<%# Eval("Anexo") %>' Text="Clique aqui para ver o anexo" Width="250px" Visible="false"  OnClick="lblMensagemAnexo_Click"></asp:LinkButton>

And then in Event, use Javascript to open a new tab.

protected void lblMensagemAnexo_Click(object sender, EventArgs e)
{
    LinkButton btn = (LinkButton)(sender);
    string url= btn.CommandArgument;
    Response.Write("<script>window.open('" +url+"','_blank');</script>");
}

Just to point out, it was the way I found to solve the problem.

  • I put wrong thing, exchange Navigateurl for Commandargument.

  • It works, but will not open in a new tab. Because target does not work in hyperlink.

  • Sorry @Marconi, I got confused. I thought it was Linkbutton, then I would have to use this procedure. For a Hyperlink, just use Target.

1


Marconi, You don’t need to use Webcontrol or javascript to do this. Instead try something simpler like:

<a href='<%#Eval("Anexo")%>' download target="_blank">Clique aqui para ver o anexo</a>
  • download forces the browser to download the contents of the link. (does not work in IE)
  • target is used as fallback

Browser other questions tagged

You are not signed in. Login or sign up in order to post.