Hiperlink + Eval + navigateURL

Asked

Viewed 142 times

0

I would like to create a link on the listview line to navigate to a url when clicked. The "city" field has to complete the url so that the direction is done correctly.

<%#Eval("cidade")%>

Example: Let’s say I want the user to be directed to wikipedia to view the clicked city information and so on, like this: https://pt.wikipedia.org/wiki/"city"

1 answer

0


In the Navigateurl of component Hyperlink that is inside a ItemTemplate of component Listview do:

NavigateUrl='<%#string.Format("https://pt.wikipedia.org/wiki/{0}", Eval("Cidade"))%>'>     

Complete code:

<form id="form1" runat="server">
    <div>
        <asp:ListView ID="ListView1" runat="server">
            <ItemTemplate>
                <asp:HyperLink 
                    ID="HyperLink1" 
                    runat="server" 
                    Text ='<%#Eval("Cidade") %>'
                    NavigateUrl='<%#string.Format("https://pt.wikipedia.org/wiki/{0}", Eval("Cidade"))%>'>                    
                </asp:HyperLink>
            </ItemTemplate>
        </asp:ListView>
    </div>
</form>

In the latest version you can do so too:

NavigateUrl='<%#$"https://pt.wikipedia.org/wiki/{Eval("cidade")}"%>'>

recourse to that call: C# 6 - String Interpolation

Browser other questions tagged

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