w128
w128

Reputation: 4928

Binding data to a hyperlink repeater - how can I control the Text attribute value of each hyperlink?

I have a hyperlink repeater that I bind with a List<string> containing some URLs:

<asp:Repeater id="linksRepeater" runat="server">
   <ItemTemplate>       
        <asp:HyperLink ID="HyperLink1" runat="server" Text='Url to ??' NavigateUrl='<%# Container.DataItem.ToString() %>' />
   </ItemTemplate>
</asp:Repeater>

// Bind data in the code behind
List<string> urls = GetUrls();
linksRepeater.DataSource = urls;
linksRepeater.DataBind();

// Example:
// urls[0] = "http://siteA.com/subsite/sub/sub2/web1.html" 
// urls[1] = "http://siteA.com/subsite/x/y/web2.html"
// urls[2] = "http://siteB.com/x/web3.html"

The problem is, using this code, all hyperlinks are of course displayed like this:

Instead, I would like to replace theText attribute of each hyperlink instance with a result of the following method call: string TransformUrl(string url) - like this:

<asp:HyperLink ID="HyperLink1" runat="server" Text='Url to <%# TransformUrl(Container.DataItem.ToString()) %>' NavigateUrl='<%# Container.DataItem.ToString() %>' />

However, this code results in an error. The TransformURL() method processes the URL and returns a new string, i.e. I'm trying to get something like:

What's the best way to do this, i.e. assign a different text attribute for each hyperlink based on the value of Container.DataItem?

Upvotes: 0

Views: 1318

Answers (1)

Andrei
Andrei

Reputation: 56688

You are almost there. The only thing to change is how your are literal string with computed value - it is better to do it inside the <%# %> tag:

Text='<%# string.Format("Url to {0}", TransformUrl(Container.DataItem.ToString())) %>'

Upvotes: 1

Related Questions