HyperLinkField with GridViews

Posted Friday, April 28, 2006 8:52 AM by C-Dog's .NET Tip of the Day

By now, you may have used the gridview to display some sort of data. Often in a gridview, you may want to display a hyperlink that links to another page and include a parameter such as an id. At first, you may be tempted to use a TemplateField and render the hyperlink yourself and use a Bind expression to craft a querystring with your custom paramters. Although that way will work, the HyperLinkField column actually handles this very well.

The HyperLinkField contains two properties to handle this DataNavigateUrlFields and DataNavigateUrlFormatString. Simply specify what fields you want to use and then build a format string like any other.

Here is an example.

<asp:HyperLinkField DataTextField="LastName" DataNavigateUrlFields="Id" 
DataNavigateUrlFormatString="/page.aspx?id={0}" />

Read the complete post at http://www.dotnettipoftheday.com/blog.aspx?id=267