The
HyperLinkField class is used by data-bound controls (such as
GridView and
DetailsView) to display a hyperlink for each record displayed.
When the user clicks a hyperlink, this user is directed to the Web page associated with the hyperlink.
To specify the caption to display for the hyperlinks, use the
Text property.
Use the
NavigateUrl property to specify the URL to navigate to when a hyperlink is clicked.
Use the
Target property to specify the target window (by default the linked content will be displayed in the existing window or frame).
The
HyperLinkField object can bound to fields in a data source. This allows developers to display a different caption for each hyperlink in the HyperLinkField object.
and to have each hyperlink navigate to a different location. To bind a field to a caption, set the
DataTextField property.
To create a URL for navigation, set the DataNavigateUrlFields property to a comma-separated list of fields to use to create the URL.
What if the developers want to change the HyperLinkField programatically? For instance, the developers want to attache the page index of Gridview to the navigation URL in the format of:
http://localhost/mysite/test.aspx?p=x
Where the "x" the current page index of the selected row.
There are two ways to do so.
- In DataBound of GridView controls.
HyperLinkField hLink = GridView1.Columns[0] as HyperLinkField;
hLink.DataNavigateUrlFormatString = "details.aspx?pi=" + GridView1.PageIndex.ToString() + "&eID={0}";
- In DataRowBound event of GridView controls.
HyperLink hLink = e.Row.Cells[0].Controls[0] as HyperLink;
hLink.NavigateUrl = "test.aspx?q=" + e.Row.Cells[0].Text;
I prefer the first option, because DataRowBound will be executed for each row, meaning less efficient. The following code shows the complete example of the first option.
<%@ Page language="C#" %>
<script runat="server">
protected void GridView1_DataBound(object sender, EventArgs e)
{
int intNext;
string strNext;
HyperLinkField hLink = GridView1.Columns[0] as HyperLinkField;
hLink.DataNavigateUrlFormatString = "details.aspx?pi=" + GridView1.PageIndex.ToString() + "&ProductID={0}";
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head runat="server">
<title>HyperLinkField Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>HyperLinkField Example</h3>
<!-- Populate the Columns collection declaratively. -->
<!-- The UnitPrice field values are bound to the -->
<!-- captions of the hyperlinks in the HyperLinkField -->
<!-- field column, formatted as currency. The ProductID -->
<!-- field values are bound to the navigate URLs of the -->
<!-- hyperlinks. However, instead of being the actual -->
<!-- URL values, the product ID is passed to the linked -->
<!-- page as a parameter in the URL specified by the -->
<!-- DataNavigateUrlFormatString property. -->
<asp:gridview id="GridView1"
datasourceid="OrdersSqlDataSource"
autogeneratecolumns="false"
OnDataBound="GridView1_DataBound"
runat="server">
<columns>
<asp:boundfield datafield="OrderID"
headertext="Order ID"/>
<asp:boundfield datafield="ProductID"
headertext="Product ID"/>
<asp:hyperlinkfield datatextfield="UnitPrice"
datatextformatstring="{0:c}"
datanavigateurlfields="ProductID"
datanavigateurlformatstring="~\details.aspx?ProductID={0}"
headertext="Price"
target="_blank" />
<asp:boundfield datafield="Quantity"
headertext="Quantity"/>
</columns>
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. -->
<asp:sqldatasource id="OrdersSqlDataSource"
selectcommand="SELECT [OrderID], [ProductID], [UnitPrice], [Quantity] FROM [Order Details]"
connectionstring="server=localhost;database=northwind;integrated security=SSPI"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>