Export (0) Print
Expand All

HyperLinkColumn Class

A column type for the DataGrid control that contains a hyperlink for each item in the column.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public class HyperLinkColumn : DataGridColumn
public class HyperLinkColumn extends DataGridColumn
public class HyperLinkColumn extends DataGridColumn

Use the HyperLinkColumn column type in a DataGrid control to create a hyperlink for each row in the DataGrid. Set the Text property to specify the caption text for the hyperlink. To specify the URL to link to when the hyperlink is clicked, set the NavigateUrl property.

NoteNote

If you set the Text and NavigateUrl properties, all hyperlinks in the column will share the same caption and URL.

You can also bind the text caption and URL of the hyperlinks in the HyperLinkColumn to a field in a data source instead of setting the Text and NavigateUrl properties. This allows you to display a different caption and to specify a different URL for each hyperlink in the column.

Use the DataTextField property to specify a field in a data source to bind to the text caption of the hyperlinks in the column.

NoteNote

The DataTextField and Text properties cannot both be set at the same time. If both properties are set, the DataTextField property takes precedence.

Specify the field to bind to the URL of the hyperlink by setting the DataNavigateUrlField property.

NoteNote

The DataNavigateUrlField and NavigateUrl properties cannot both be set at the same time. If both properties are set, the DataNavigateUrlField property takes precedence.

The Target property allows you to specify the window or frame to display the Web page content linked to when the hyperlink is clicked.

When using data binding, you can format the text caption and URL of the hyperlink by setting the DataTextFormatString and DataNavigateUrlFormatString properties, respectively.

Caution noteCaution

This control can be used to display user input, which might include malicious client script. Check any information that is sent from a client for executable script, SQL statements, or other code before displaying it in your application. You can use validation controls to verify user input before displaying the input text in a control. ASP.NET provides an input request validation feature to block script and HTML in user input. For more information, see Securing Standard ControlsHow to: Protect Against Script Exploits in a Web Application by Applying HTML Encoding to Strings, and Validating User Input in ASP.NET Web Pages.

The following example demonstrates how to create a HyperLinkColumn that links to a separate page.

NoteNote

The following code sample uses the single-file code model and may not work correctly if copied directly into a code-behind file. This code sample must be copied into an empty text file that has an .aspx extension. For more information on the Web Forms code model, see ASP.NET Web Page Code Model.

<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

<html>

<head>

   <script runat="server">

      ICollection CreateDataSource() 
      {
         DataTable dt = new DataTable();
         DataRow dr;

         dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
         dt.Columns.Add(new DataColumn("PriceValue", typeof(Double)));
       
         for (int i = 0; i < 3; i++) 
         {
            dr = dt.NewRow();

            dr[0] = i;
            dr[1] = (Double)i * 1.23;

            dt.Rows.Add(dr);
         }

         DataView dv = new DataView(dt);
         return dv;
      }

      void Page_Load(Object sender, EventArgs e) 
      {
         MyDataGrid.DataSource = CreateDataSource();
         MyDataGrid.DataBind();
      }

   </script>

</head>

<body>

   <form runat="server">

      <h3>HyperLinkColumn Example<h3>

      <asp:DataGrid id="MyDataGrid" 
           BorderColor="black"
           BorderWidth="1"
           GridLines="Both"
           AutoGenerateColumns="false"
           runat="server">

         <HeaderStyle BackColor="#aaaadd"/>

         <Columns>

            <asp:HyperLinkColumn
                 HeaderText="Select an Item"
                 DataNavigateUrlField="IntegerValue"
                 DataNavigateUrlFormatString="detailspage.aspx?id={0}"
                 DataTextField="PriceValue"
                 DataTextFormatString="{0:c}"
                 Target="_blank"/>
           
         </Columns>

      </asp:DataGrid>

   </form>

</body>
</html>


The following corresponding example displays the item selected in the previous example.

<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
 
   <script runat="server">
 
      void Page_Load(Object sender, EventArgs e) 
      {
         Label1.Text = "You selected item: " + Request.QueryString["id"];
      }
 
   </script>
 
</head>
<body>
 
   <h3>Details page for DataGrid</h3>
 
   <asp:Label id="Label1"
        runat="server"/>
 
</body>
</html>


System.Object
   System.Web.UI.WebControls.DataGridColumn
    System.Web.UI.WebControls.HyperLinkColumn

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft