Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a ListView that I am binding to a SPList. In this list view I would like to have a link to each SPListItem.

Microsoft has conveniently decided that the URL member of an SPListItem should be a mysterious link that does not work. After some research I found some other people that had run into this and used the following work around:

listItem.ParentList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url + "?ID=" + listItem.ID

I attempted to shoe horn this into my ListView ItemTemplate and it almost works (using 0 instead of a PAGETYPE):

        <ItemTemplate>
            <a id="a_ItemLink" href="<%# DataBinder.GetIndexedPropertyValue(((SPListItem)Container.DataItem).ParentList.Forms, "[0].Url")  + "?ID=" + Eval("ID") %>" runat="server"><%# Eval("Title") %></a><br />
        </ItemTemplate>

The issue that GetIndexedPropertyValue sets my links to something like this:

http://mySpSIte/sites/CDA/SitePages/Microsoft.SharePoint.SPForm?ID=1

I'm not sure why it would include the Microsoft.SharePoint.SPForm part, but obviously this is not working. Has anyone successfully DataBound to a SPListItem and extracted the URL? Am I doing something wrong above?

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

If you have items from one list, you can use this solution:

// Column with link
var hyperLinkField = new HyperLinkField();
hyperLinkField.HeaderText = "Display item link";
hyperLinkField.Text = "Show";
hyperLinkField.DataNavigateUrlFields = new string[] { "ID" };
hyperLinkField.DataNavigateUrlFormatString = list.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url + "?ID={0}";
gridView.Columns.Add(hyperLinkField);

This was tested and works with SPGridView and GridView.

That code should be similar to this aspx markup:

<asp:HyperLinkField 
  HeaderText="Display item link"
  Text="Show"
  DataNavigateUrlFields="ID"
  DataNavigateUrlFormatString="<%= list.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url %>?ID={0}"
   />

MSDN reference for HyperLinkField:

share|improve this answer
add comment

It's casting properly (which is why you're seeing Microsoft.SharePoint.SPForm), but it's not getting the property that you're trying to set. What exactly does the [0] correspond to? Can you try "[\"Url\"]" instead? I'm getting that from this link.

Edit: Aaaand I just noticed that the link is to your own post :)

share|improve this answer
    
Haha, yeah similar issue. My Problem is that I got that working and the URL that is returned doesn't work. Apparently this is the expected functionality (I would like to hear an explanation from Microsoft on that one....) –  Abe Miessler May 23 '11 at 18:04
add comment

If you are trying to pass more than one fields in target url, you can simply add one more field in the DataNavigateUrlFields array, like this:

hyperLinkField.DataNavigateUrlFields = new string[] { "ContentFolder", "ID" };

What I just mentioned can be an obvious thing for a smart developer, but I just wanted to say it out loud because I was trying to find the solution to allow multiple fields and when I came across this solution, I wondered, what if I do that, would it help? and it actually did.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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