Search code examples
asp.nethyperlinkhrefservercontrols

Dynamically adding href target and open that in new tab


My Code is:

 <a href='<%# Eval("websitename")%>' runat="server" target="_blank">
 <asp:Label runat="server" Text='<%# Eval("websitename")%>'></asp:Label>
 </a>

I will bind website name from code behind, when i click that it searches that website inside my local host.

if that website name is "www.stackoverflow.com" it open like "localhost:1234/www.stackoverflow.com

How to redirect to www.stackoverflow.com.

Thanks in Advance


Solution

  • Check if websitename starts with http:// and if not, add it.

    Or if websitename never starts with http://, just do

    <a href='<%# "http://" + Eval("websitename")%>'