View Single Post
  #2 (permalink)  
Old January 16th, 2011, 05:36 AM
Imar's Avatar
Imar Imar is offline
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts

Hi there,

You have a few options. One is to do it visually using the Edit Columns dialog of the GridView. In the Available Fields you can choose a HyperLinkField and then use properties such as DataNavigateUrlFields, DataNavigateUrlFormatString, and DataTextField to format the output. You should end up with something like this:

<asp:GridView ....>
    <asp:HyperLinkField DataNavigateUrlFields="Id" DataNavigateUrlFormatString="Default.aspx?Id={0}" DataTextField="Name" HeaderText="View" />
Alternatively, you can convert a column into a TemplateField (again using the Edit Columns dialog), and manually build up a HyperLink like this:

<asp:GridView ....>
    <asp:TemplateField HeaderText="View">
        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# string.Format("Default.aspx?Id={0}", Eval("Id")) %>' Text='<%# Eval("Name") %>' />
The latter is closer to what you're used to doing in classic ASP. Additionally, it gives you more control and flexibility over the output.

In both cases, Id and Name refer to columns from the database.

Hope this helps,

Imar Spaanjaars
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
murshed (January 16th, 2011)