Question

I have anchor tag inside Data List control. I am binding DataList in code behind and it is working fine. But problem is that I have kept link(url) field in database. To redirect user to this url, I am binding anchor tag with this field. But it is not giving correct address. It is appending my local host(website address) to it at start which I don't want. My code is as below:

<asp:DataList ID="dlPost" runat="server" RepeatColumns="1" RepeatDirection="Vertical"
    Width="755px">
    <ItemTemplate>
        <article class="post medium">

             <div class="medium-content">

                  <header class="meta">
                 <h2> <a href ="#"><asp:Label ID="lblTitle" runat="server" Text='<%#Eval("PostTitle") %>' style="font-weight:600; color:#444444;"></asp:Label></a></h2>
                 </header>
                 <p> <asp:Label ID="lblPost" runat="server" Text='<%#Eval("Post") %>'></asp:Label></p>
                 <a href='<%#Eval("PostLink") %>' class="button color">Job Link</a>
             </div>
         </article>
         <div class="line"></div>
    </ItemTemplate>
</asp:DataList>

Below is the part where I am trying to bind url field of database with anchor tag.

<a href='<%#Eval("PostLink") %>' class="button color">Job Link</a>

So, if Poslink field of database contains www.abc.com, it is redirecting it to http://localhost:54636/CKWeb/www.abc.com instead of www.abc.com

Was it helpful?

Solution

You should use http:// for making it working. Your dynamic href should be binded using

'<%# string.Format("http://{0}",Eval("link"))%>'

Using above ensure your link does not contain http:// else it will not work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top