Search code examples
javascriptjqueryasp.netlabelhref

setting label to link in JavaScript


I'm trying to set a label to a link to open an image. I was using asp.net in the code behind to do this:

lblFile1.Text = "<a href=\"/utilities/ViewPDF.aspx?ImageName=" + ConfigurationManager.AppSettings["Logo2ImageFolder"] + Company.Current.CompCode + "\\" + f.File1 + "\" target=\"blank\">View File</a>";

But now I need to change this to JavaScript, so when they click link the label it opens the link.

I tried this but the label doesn't even display:

document.getElementById('lblFile1').value = "<a href=\"/utilities/ViewPDF.aspx?ImageName= + ConfigurationManager.AppSettings['Logo2ImageFolder'] + Company.Current.CompCode + '\\' + " + result.File1 + "\" target=\"blank\">View File</a>";

I'm using <asp:Label runat="server" ID="lblFile1"></asp:Label>

The link did work when I was using it in the code behind but it's not working in JavaScript.


Solution

  • Change

    document.getElementById('lblFile1').value
    

    to

    document.getElementById('lblFile1').innerHTML
    

    This allows the contents of your label (an HTML span) to interpret the HTML link you place inside.

    Also check your output because ASP.NET may be changing your asp:Label ID at output. Setting the ClientIDMode to "static" will solve the issue. Make sure your chosen ID doesn't conflict with any other nodes having the same ID.