<asp:HyperLinkField> Control
The <asp:HyperLinkField> control appears in a GridView
as linking text for each record displayed. Clicking the link directs to a Web page.
A HyperLinkField is coded inside the <Columns> section along
with other bound field controls. Properties associated with this control are shown in
Figure 7-10.
Figure 7-10. General format for <asp:HyperLinkField> control of <asp:GridView> control.
When the NavigateUrl property is supplied, all records share
the same URL. The associated Text property sets the displayed text
that serves as the clickable link. The Target property specifies
the window or frame in which to open the linked page. Its values can be
"_blank" to open the page in a new browser window, "_self" to
open the page in the current window, "_search" to open the page in the
window's search pane, "_top" to open the page in the
current full browser window, "_parent" to open the page in the next
higher-level window of a frameset, or the name of a frame in the current window.
Rather than using the same link for all records, you can bind the link to fields in the GridView's
data source to link to different pages. The caption for the link is set by the
DataTextField property, supplying its format through the
DataTextFormatString property. The URL is composed from the
DataNavigateUrlFields property, with formatting supplied by the
DataNavigateUrlFormatString property. As in the case with the ImageField control,
the "{0}" format string serves as a placeholder for inserting values
inside a text string coded as the DataTextFormatString, and inside a
URL coded as the DataNavigateUrlFormatString.
A GridView table that displays links for records from the BooksDB.mdb
database is shown below. In this case, a link is made to an image file which displays in an
<iframe> coded on the page.
Figure 7-11. Using a HyperLinkField column in a GridView.
<asp:AccessDataSource id="BookSource" Runat="Server"
DataFile="../Databases/BooksDB.mdb"
SelectCommand="SELECT BookID, BookTitle FROM Books
WHERE BookType='Web' ORDER BY BookID"/>
<asp:GridView id="BookGrid" DataSourceID="BookSource" Runat="Server"
Style="float:left"
AutoGenerateColumns="False"
CellPadding="3"
HeaderStyle-BackColor="#707070"
HeaderStyle-ForeColor="#FFFFFF">
<Columns>
<asp:BoundField
DataField="BookID"
HeaderText="ID"/>
<asp:BoundField
DataField="BookTitle"
HeaderText="Title"/>
<asp:HyperLinkField
HeaderText="Photo Link"
DataTextField="BookID"
DataTextFormatString="Picture of {0}"
DataNavigateUrlFields="BookID"
DataNavigateUrlFormatString="../BookPictures/{0}.jpg"
Target="PictureFrame"
ItemStyle-Font-Size="10pt"/>
</Columns>
</asp:GridView>
<iframe name="PictureFrame" style="width:120px; height:145px"></iframe>
Listing 7-7. Code for GridView with HyperLinkField column.
The BookID from the associated record is inserted into the
DataTextFormatString to produce a text link. In a similar manner,
the BookID is inserted into the
DataNavigateUrlFormatString to produce a URL for the picture relative
to the current page.
The target for the link is an <iframe> tag named
PictureFrame appearing along side the GridView table. This positioning
requires the GridView to be floated to the left margin of the page by adding a CSS style
setting to the GridView: Style="float:left". A Target
property is added to the HyperLinkField specifying the frame name as the target for
the URL. Notice that the <iframe> tag does not require an
id or a Runat="Server" attribute in order to be
accessible by the HyperLinkField.