asp.netdata-bindingrepeaterdatabinder

Why is the link set wrong in my databound repeater?


I want to show email links on my repeater (if the user has elected to have his/her email address displayed) but all I'm getting is the Databinder.Eval(Container.DataItem, "emailaddress") writing to the actual link. It isn't being parsed as it should...

Here's my markup:

<asp:Repeater runat="server" ID="rptGuestbook" OnItemCommand="rptGuestbook_ItemCommand"
    OnItemDataBound="rptGuestbook_ItemDataBound" DataSource="<%# guestbookData %>">
    <ItemTemplate>
        <div runat="server" id="divPost" class="post">
            <div runat="server" id="divAuthor" class="author">
                <asp:Panel runat="server" ID="pnlAdmin" CssClass="delete-button" Visible="false">
                    <asp:Button runat="server" ID="btnDeletePost" CssClass="button" Text="Delete Post"
                        CommandName="DeletePost" CommandArgument='<%# Eval("postid") %>' />
                    <asp:Label runat="server" ID="lblShowEmail" Text='<%# Eval("showemail") %>' CssClass="hidden"></asp:Label>
                </asp:Panel>
                <ul>
                    <li>#<%# Eval("postid") %></li>
                    <li><span>Posted By: </span>
                        <%# Eval("postauthor") %><br />
                        <li>
                            <asp:HyperLink runat="server" ID="lnkWeb" ImageUrl="~/Images/webicon.png" NavigateUrl='<%# Eval("webaddress") %>'></asp:HyperLink></li>
                        <li>
                            <asp:HyperLink runat="server" ID="lnkEmail" ImageUrl="~/Images/emailicon.png" NavigateUrl='mailto: <%# DataBinder.Eval(Container.DataItem, "emailaddress" %>'></asp:HyperLink></li>
                </ul>
            </div>
            <div runat="server" id="divMessage" class="post-message">
                <div class="post-date">
                    <span>Posted On:</span>
                    <%# Eval("postdate") %></div>
                <%# Eval("postmessage") %></div>
            <div class="scroll">
            </div>
        </div>
    </ItemTemplate>
</asp:Repeater>

<asp:HyperLink runat="server" ID="lnkEmail" ImageUrl="~/Images/emailicon.png" NavigateUrl='mailto: <%# DataBinder.Eval(Container.DataItem, "emailaddress" %>'></asp:HyperLink>

And here is exactly what my browser thinks the link is...

%20<%#%20DataBinder.Eval(Container.DataItem,%20"emailaddress"%20%>

I've checked and every row has an email address in it so I really don't understand why this is happening.

Can anyone help me to fix it?

Thanks in advance!


Solution

  • Try changing your mailto HyperLink from this:

    <asp:HyperLink runat="server" 
                   ID="lnkEmail" 
                   ImageUrl="~/Images/emailicon.png" 
                   NavigateUrl='mailto: <%# DataBinder.Eval(Container.DataItem, "emailaddress" %>'></asp:HyperLink>
    

    to this

    <asp:HyperLink runat="server" 
                   ID="lnkEmail" 
                   ImageUrl="~/Images/emailicon.png" 
                   NavigateUrl='<%# "mailto:" + Eval("emailaddress") %>'
                   Text='<%# Eval("emailaddress") %>'></asp:HyperLink>
    

    as user256103 mentioned you were missing a closing paren in your DataBinder.Eval. I think the larger issue however is the value of NavigateUrl. ASP.NET's data binding scriplet (<%# %>) allows strings to be concatenated. You'll also need a Text value for the HyperLink server control to work.