6

I am making an ASP.NET control which displays a list of links embedded in a containing <ul/>. Based on a taxonomy query, I'm going to add a list of Tridion.ContentDelivery.Web.UI.ComponentLink objects to the control tree.

I would also like to deal with the situation where none of the links resolve. I could do this by also instantiating a matching list of Tridion.ContentDelivery.Web.Linking.ComponentLink, calling GetLink() and evaluating the IsResolved property. However this feels inelegant, and leaves me wondering if there isn't a tidier way.

Have I missed something, or is this the only way?

1
  • Of course there are other ways. :-) JavaScript "clean up," changing the content model (maybe there's a link that's always published), or perhaps changing these to component presentations are a few not-so-elegant approaches.
    – Alvin Reyes
    Sep 30 '13 at 14:00
9

Apart from the empty <ul/> you are also going to face the issue of empty <li/> tags if individual links do not resolve, so you will be better off going the list of Tridion.ContentDelivery.Web.Linking.ComponentLink option.

2
  • I have to agree with Will here, however inelegant it looks like it is the only option you have to resolve all links and ten determine if you need to show them. The good thing is that links are cached, so you don't have to worry about it too much. Sep 30 '13 at 13:42
  • I ended up doing exactly this.
    – Dominic Cronin
    Oct 2 '13 at 14:53
6

Another approach is to "extend" your tcdl:Link Renderer (REL) and make sure that if the link is not resolved, you don't get those extra html elements... Unfortunately there's not an elegant way of avoiding the li/ul empty elements other than taking care of them by evaluating whether the link has or has not been resolved prior to rendering them. To me the "elegant" way is using the renderer.

4
  • How do you integrate REL executed code into a .NET page using a .NET control?
    – Will Price
    Sep 30 '13 at 14:11
  • REL gets executed "internally" within the Component Presentation Assembler or the Page Content Assembler, so if you .Net Control uses those classes it is integrated by default. So, you cannot do it at a link level but a Component Presentation level where the links are displayed. Sep 30 '13 at 15:59
  • So I guess this only works for dynamic component presentations? and in that case your DCP could not contain .NET code as it would be a REL DCP rather than an ASCX...
    – Will Price
    Oct 1 '13 at 8:13
  • That is correct. Oct 1 '13 at 12:05
6

Another approach would be to use a Templated Web Control. You can have something similar to this.

<li>
    <asp:ResolvedAwareLink runnat="server" ComponentUri="[TcmUri]">
        <Resolved>
            <a href="<%# Container.Link%>"><%# Container.LinkText %></a>
        </Resolved>
        <NotResolved>
            <!-- Some optional Logic -->
        </NotResolved>
    </asp:ResolvedAware>
</li>

or a Template Data Bound Web Control.

<asp:ResolvedAwareLinks ID="links" runnat="server">
    <ResolvedTemplate>
        <li>
            <a href="<%# Container.Link%>"><%# Container.LinkText %></a>
        </li>
    </ResolvedTemplate>
</asp:ResolvedAware>

C#:

links.DataSource = GetLinksFromTaxonomy().Where(w => w.IsResolved());
links.DataBind();

The implementation of this Templated Web Control will need to use the Component Link API to populate properties like Link and LinkText.

5
  • But then you still would have your <li> tags surrounding it, even if you don't want to have some 'Not Resolved Logic' text right?
    – Raimond
    Sep 30 '13 at 14:41
  • Yes, in that case you can put the <li> tag inside the Resolved Template.
    – Eric Huiza
    Sep 30 '13 at 14:46
  • Thanks Eric - but I'm not really clear how this would work. How would I control the ul?
    – Dominic Cronin
    Sep 30 '13 at 15:08
  • I updated my answer to use a Templated Data Bound Web Control, you can assign a Data Source to the control and in case there is no links resolved then even the <ul> tags will not appear. I hope this helps.
    – Eric Huiza
    Sep 30 '13 at 15:27
  • Would this be a good description of Data-Bound Web Server Controls? There's an older tutorial here as well. I think you could also the <li> into a parameter to make it more generic.
    – Alvin Reyes
    Sep 30 '13 at 15:41
3

You can also use some of the attributes on the link controls themselves to control how the links are displayed. For instance, TextOnFail='false' will make the web control not render anything if the link isn't resolved.

Here is the link to the list of all supported attributes (login required): http://sdllivecontent.sdl.com/LiveContent/content/en-US/SDL_Tridion_2013/concept_ED500433FB5C41F4BFBF7746835A6EAA

2
  • TextOnFail is handy, but will still leave you with empty li tags surrounding the place where the link would have been if it had resolved
    – Will Price
    Oct 1 '13 at 8:15
  • TextOnFail is useful in running text, but not in link lists etc.
    – Dominic Cronin
    Oct 1 '13 at 22:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.