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

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:

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

Code:
 
<asp:GridView ....>
  <Columns>
    ...
    <asp:TemplateField HeaderText="View">
      <ItemTemplate>
        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# string.Format("Default.aspx?Id={0}", Eval("Id")) %>' Text='<%# Eval("Name") %>' />
      </ItemTemplate>
    </asp:TemplateField>
    ...
  </Columns>
</asp:GridView>
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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
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)