HyperLinkColumn HyperLinkColumn HyperLinkColumn HyperLinkColumn Class

Definition

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

public ref class HyperLinkColumn : System::Web::UI::WebControls::DataGridColumn
public class HyperLinkColumn : System.Web.UI.WebControls.DataGridColumn
type HyperLinkColumn = class
    inherit DataGridColumn
Public Class HyperLinkColumn
Inherits DataGridColumn
Inheritance
HyperLinkColumnHyperLinkColumnHyperLinkColumnHyperLinkColumn

Examples

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

Note

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 Forms Page Code Model.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

<head>
    <title>HyperLinkColumn Example</title>
<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 id="form1" 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>

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

<head>
    <title>HyperLinkColumn Example</title>
<script runat="server">

      Function CreateDataSource() As ICollection 
      
         Dim dt As DataTable = New DataTable()
         Dim dr As DataRow
         Dim i As Integer

         dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
         dt.Columns.Add(New DataColumn("PriceValue", GetType(Double)))
       
         For i = 0 to 2 
         
            dr = dt.NewRow()

            dr(0) = i
            dr(1) = CDbl(i) * 1.23

            dt.Rows.Add(dr)

         Next i

         Dim dv As DataView = New DataView(dt)
         Return dv

      End Function

      Sub Page_Load(sender As Object, e As EventArgs) 
    
         MyDataGrid.DataSource = CreateDataSource()
         MyDataGrid.DataBind()

      End Sub

   </script>

</head>

<body>

   <form id="form1" 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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Details page for DataGrid</title>
<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>

<%@ Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Details page for DataGrid</title>
<script runat="server">
 
      Sub Page_Load(sender As Object, e As EventArgs) 
      
         Label1.Text = "You selected item: " & Request.QueryString("id")
      
      End Sub
 
   </script>
 
</head>
<body>
 
   <h3>Details page for DataGrid</h3>
 
   <asp:Label id="Label1"
        runat="server"/>
 
</body>
</html>

Remarks

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.

Note

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.

Note

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.

Note

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

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 Controls, How to: Protect Against Script Exploits in a Web Application by Applying HTML Encoding to Strings, and Validating User Input in ASP.NET Web Pages.

Constructors

HyperLinkColumn() HyperLinkColumn() HyperLinkColumn() HyperLinkColumn()

Initializes a new instance of the HyperLinkColumn class.

Properties

DataNavigateUrlField DataNavigateUrlField DataNavigateUrlField DataNavigateUrlField

Gets or sets the field from a data source to bind to the URL of the hyperlinks in the HyperLinkColumn.

DataNavigateUrlFormatString DataNavigateUrlFormatString DataNavigateUrlFormatString DataNavigateUrlFormatString

Gets or sets the display format for the URL of the hyperlinks in the HyperLinkColumn when the URL is data-bound to a field in a data source.

DataTextField DataTextField DataTextField DataTextField

Gets or sets the field from a data source to bind to the text caption of the hyperlinks in the HyperLinkColumn.

DataTextFormatString DataTextFormatString DataTextFormatString DataTextFormatString

Gets or sets the display format for the text caption of the hyperlinks in the HyperLinkColumn column.

DesignMode DesignMode DesignMode DesignMode

Gets a value that indicates whether the column is in design mode.

(Inherited from DataGridColumn)
FooterStyle FooterStyle FooterStyle FooterStyle

Gets the style properties for the footer section of the column.

(Inherited from DataGridColumn)
FooterText FooterText FooterText FooterText

Gets or sets the text displayed in the footer section of the column.

(Inherited from DataGridColumn)
HeaderImageUrl HeaderImageUrl HeaderImageUrl HeaderImageUrl

Gets or sets the location of an image to display in the header section of the column.

(Inherited from DataGridColumn)
HeaderStyle HeaderStyle HeaderStyle HeaderStyle

Gets the style properties for the header section of the column.

(Inherited from DataGridColumn)
HeaderText HeaderText HeaderText HeaderText

Gets or sets the text displayed in the header section of the column.

(Inherited from DataGridColumn)
IStateManager.IsTrackingViewState IStateManager.IsTrackingViewState IStateManager.IsTrackingViewState IStateManager.IsTrackingViewState

Gets a value that indicates whether the column is tracking view state changes.

(Inherited from DataGridColumn)
IsTrackingViewState IsTrackingViewState IsTrackingViewState IsTrackingViewState

Gets a value that determines whether the DataGridColumn object is marked to save its state.

(Inherited from DataGridColumn)
ItemStyle ItemStyle ItemStyle ItemStyle

Gets the style properties for the item cells of the column.

(Inherited from DataGridColumn)
NavigateUrl NavigateUrl NavigateUrl NavigateUrl

Gets or sets the URL to link to when a hyperlink in the column is clicked.

Owner Owner Owner Owner

Gets the DataGrid control that the column is a member of.

(Inherited from DataGridColumn)
SortExpression SortExpression SortExpression SortExpression

Gets or sets the name of the field or expression to pass to the OnSortCommand(DataGridSortCommandEventArgs) method when a column is selected for sorting.

(Inherited from DataGridColumn)
Target Target Target Target

Gets or sets the target window or frame to display the Web page content that is linked to when the hyperlink in the column is clicked.

Text Text Text Text

Gets or sets the text caption to display for the hyperlinks in the column.

ViewState ViewState ViewState ViewState

Gets the StateBag object that allows a column derived from the DataGridColumn class to store its properties.

(Inherited from DataGridColumn)
Visible Visible Visible Visible

Gets or sets a value that indicates whether the column is visible in the DataGrid control.

(Inherited from DataGridColumn)

Methods

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
FormatDataNavigateUrlValue(Object) FormatDataNavigateUrlValue(Object) FormatDataNavigateUrlValue(Object) FormatDataNavigateUrlValue(Object)

Formats a data-bound URL using the format specified by the DataNavigateUrlFormatString property.

FormatDataTextValue(Object) FormatDataTextValue(Object) FormatDataTextValue(Object) FormatDataTextValue(Object)

Formats a data-bound text caption using the format specified by the DataTextFormatString property.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
Initialize() Initialize() Initialize() Initialize()

Provides the base implementation to reset a column derived from the DataGridColumn class to its initial state.

InitializeCell(TableCell, Int32, ListItemType) InitializeCell(TableCell, Int32, ListItemType) InitializeCell(TableCell, Int32, ListItemType) InitializeCell(TableCell, Int32, ListItemType)

Initializes the cell representing this column with the contained hyperlink.

LoadViewState(Object) LoadViewState(Object) LoadViewState(Object) LoadViewState(Object)

Loads the state of the DataGridColumn object.

(Inherited from DataGridColumn)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
OnColumnChanged() OnColumnChanged() OnColumnChanged() OnColumnChanged()

Calls the OnColumnsChanged() method.

(Inherited from DataGridColumn)
SaveViewState() SaveViewState() SaveViewState() SaveViewState()

Saves the current state of the DataGridColumn object.

(Inherited from DataGridColumn)
ToString() ToString() ToString() ToString()

Returns the string representation of the column.

(Inherited from DataGridColumn)
TrackViewState() TrackViewState() TrackViewState() TrackViewState()

Causes tracking of view-state changes to the server control so they can be stored in the server control's StateBag object.

(Inherited from DataGridColumn)

Explicit Interface Implementations

IStateManager.LoadViewState(Object) IStateManager.LoadViewState(Object) IStateManager.LoadViewState(Object) IStateManager.LoadViewState(Object)

Loads previously saved state.

(Inherited from DataGridColumn)
IStateManager.SaveViewState() IStateManager.SaveViewState() IStateManager.SaveViewState() IStateManager.SaveViewState()

Returns an object containing state changes.

(Inherited from DataGridColumn)
IStateManager.TrackViewState() IStateManager.TrackViewState() IStateManager.TrackViewState() IStateManager.TrackViewState()

Starts tracking state changes.

(Inherited from DataGridColumn)

Applies to

See also