Pages

July 14, 2013

Asp:HyperLinkField Gives Undesirable URL

Problem:

When using the ASP.NET HyperLinkField to display the SharePoint Hyperlink field, the HyperLinkField will concatenate the web address (URL) with a comma, a space and then the description.

<asp:HyperLinkField HeaderText="Detail" DataNavigateUrlFields="Detail" Text="Detail" />

The redundant portions are highlighted in Yellow.

Example1:

Result link: http://sptest/Site1/Pages/MyRequest.aspx?RequestID=2/22/2013 2:35:06 PM, View



Example2:

Result link on GridView:   http://www.google.com,%20http//www.google.com



Solution:

Replace the HyperLinkField with a TemplateField control. The TemplateField control will host a hyperlink control where it manipulate the NavigateUrl parameter to extract only the actual URL before the comma ‘,’ from the SharePoint Hyperlink field.

Example:
<asp:TemplateField HeaderText="Detail">
<ItemTemplate>
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# ((string)Eval("Detail")).Split(new string[]{","}, StringSplitOptions.None)[0] %>' Text="View" Target="_blank" />
</ItemTemplate>
</asp:TemplateField>