HyperLinkField 类

定义

表示将显示为数据绑定控件中的超链接的字段。Represents a field that is displayed as a hyperlink in a data-bound control.

public ref class HyperLinkField : System::Web::UI::WebControls::DataControlField
public class HyperLinkField : System.Web.UI.WebControls.DataControlField
type HyperLinkField = class
    inherit DataControlField
Public Class HyperLinkField
Inherits DataControlField
继承
HyperLinkField

示例

下面的代码示例演示如何使用 HyperLinkField 对象在 GridView 控件中显示静态超链接的列。The following code example demonstrates how to use a HyperLinkField object to display a column of static hyperlinks in a GridView control. HyperLinkField 对象中的每个超链接分别共享由 TextNavigateUrl 属性指定的相同标题和导航 URL。Each hyperlink in the HyperLinkField object shares the same caption and navigation URL specified by the Text and NavigateUrl properties, respectively.


<%@ Page language="C#" %>

<!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 runat="server">
    <title>HyperLinkField Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>HyperLinkField Example</h3>
                    
      <!-- Populate the Columns collection declaratively. -->
      <!-- Set the HyperLinkField field column to a static     -->
      <!-- caption and URL.                                    -->
      <asp:gridview id="OrdersGridView" 
        datasourceid="OrdersSqlDataSource" 
        autogeneratecolumns="false"
        runat="server">
                
        <columns>
                
          <asp:boundfield datafield="OrderID" 
            headertext="OrderID"/>
          <asp:boundfield datafield="CustomerID" 
            headertext="Customer ID"/>
          <asp:boundfield datafield="OrderDate" 
            headertext="Order Date"
            dataformatstring="{0:d}" />
          <asp:hyperlinkfield text="Details..."
            navigateurl="~\details.aspx"            
            headertext="Order Details"
            target="_blank" />
                
        </columns>
                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Northwind sample database.                   -->
      <asp:sqldatasource id="OrdersSqlDataSource"  
        selectcommand="SELECT [OrderID], [CustomerID], [OrderDate] FROM [Orders]"
        connectionstring="server=localhost;database=northwind;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>


<%@ Page language="VB" %>

<!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 runat="server">
    <title>HyperLinkField Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>HyperLinkField Example</h3>
                    
      <!-- Populate the Columns collection declaratively. -->
      <!-- Set the HyperLinkField field column to a static     -->
      <!-- caption and URL.                                    -->
      <asp:gridview id="OrdersGridView" 
        datasourceid="OrdersSqlDataSource" 
        autogeneratecolumns="false"
        runat="server">
                
        <columns>
                
          <asp:boundfield datafield="OrderID" 
            headertext="OrderID"/>
          <asp:boundfield datafield="CustomerID" 
            headertext="Customer ID"/>
          <asp:boundfield datafield="OrderDate" 
            headertext="Order Date"
            dataformatstring="{0:d}" />
          <asp:hyperlinkfield text="Details..."
            navigateurl="~\details.aspx"            
            headertext="Order Details"
            target="_blank" />
                
        </columns>
                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Northwind sample database.                   -->
      <asp:sqldatasource id="OrdersSqlDataSource"  
        selectcommand="SELECT [OrderID], [CustomerID], [OrderDate] FROM [Orders]"
        connectionstring="server=localhost;database=northwind;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

下面的代码示例演示如何将 HyperLinkField 对象绑定到数据源中的字段。The following code example demonstrates how to bind a HyperLinkField object to fields in a data source. DataTextFieldDataNavigateUrlFields 属性用于指定要分别绑定到 HyperLinkField 对象中显示的每个超链接的标题和导航 URL 的字段。The DataTextField and DataNavigateUrlFields properties are used to specify the fields to bind to the caption and the navigation URL, respectively, of each hyperlink displayed in the HyperLinkField object.


<%@ Page language="C#" %>

<!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 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="OrdersGridView" 
        datasourceid="OrdersSqlDataSource" 
        autogeneratecolumns="false"
        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>


<%@ Page language="VB" %>

<!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 runat="server">
    <title>HyperLinkField DataTextFormatString and DataNavigateUrlFormatString Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>HyperLinkField DataTextFormatString and DataNavigateUrlFormatString 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="OrdersGridView" 
        datasourceid="OrdersSqlDataSource" 
        autogeneratecolumns="false"
        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>

注解

数据绑定控件(如 GridViewDetailsView)使用 HyperLinkField 类来显示每个显示的记录的超链接。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, he or she is directed to the Web page associated with the hyperlink. 根据使用该对象的数据绑定控件,HyperLinkField 对象的显示方式有所不同。The HyperLinkField object is displayed differently depending on the data-bound control in which it is used. 例如,GridView 控件以列的形式显示 HyperLinkField 对象,DetailsView 控件将其显示为一行。For example, the GridView control displays a HyperLinkField object as a column, while the DetailsView control displays it as a row.

若要指定要为超链接显示的标题,请使用 Text 属性。To specify the caption to display for the hyperlinks, use the Text property. 使用 NavigateUrl 属性指定在单击超链接时要导航到的 URL。Use the NavigateUrl property to specify the URL to navigate to when a hyperlink is clicked. 如果要在特定窗口或框架中显示链接的内容,请设置 "Target" 属性。If you want to display the linked content in a specific window or frame, set the Target property.

备注

设置 TextNavigateUrl 属性时,HyperLinkField 对象中的所有超链接共享相同的标题和导航 URL。When the Text and NavigateUrl properties are set, all hyperlinks in the HyperLinkField object share the same caption and navigation URL. 同样,"Target" 属性也适用于所有超链接。Likewise, the Target property also applies to all hyperlinks.

或者,您可以将 HyperLinkField 对象绑定到数据源中的字段。Alternatively, you can bind the HyperLinkField object to fields in a data source. 这允许您为 HyperLinkField 对象中的每个超链接显示不同的标题,并使每个超链接导航到不同的位置。This allows you to display a different caption for each hyperlink in the HyperLinkField object and to have each hyperlink navigate to a different location. 若要将字段绑定到标题,请设置 DataTextField 属性。To bind a field to a caption, set the DataTextField property. 若要创建用于导航的 URL,请将 DataNavigateUrlFields 属性设置为用于创建 URL 的以逗号分隔的字段列表。To create a URL for navigation, set the DataNavigateUrlFields property to a comma-separated list of fields to use to create the URL.

通过分别设置 "DataTextFormatString" 和 "DataNavigateUrlFormatString" 属性,可以指定标题和导航 Url 的自定义格式。You can specify a custom format for the captions and navigation URLs by setting the DataTextFormatString and DataNavigateUrlFormatString properties, respectively.

可以通过将 Visible 属性设置为 false,隐藏数据绑定控件中的 HyperLinkField 对象。You can hide a HyperLinkField object in a data-bound control by setting the Visible property to false.

您可以自定义 HyperLinkField 对象的页眉和页脚部分。You can customize the header and footer sections of a HyperLinkField object. 若要在页眉或页脚部分显示标题,请分别设置 "HeaderText" 或 "FooterText 属性"。To display a caption in the header or footer sections, set the HeaderText or FooterText properties, respectively. 若要在页眉节中显示图像而不是文本,请设置 HeaderImageUrl 属性。To display an image in the header section instead of text, set the HeaderImageUrl property. 通过将 ShowHeader 属性设置为 false,可以在 HyperLinkField 对象中隐藏标头部分。The header section can be hidden in the HyperLinkField object by setting the ShowHeader property to false.

备注

某些数据绑定控件(如 GridView 控件)只能显示或隐藏控件的整个标题部分。Some data-bound controls (such as the GridView control) can show or hide only the entire header section of the control. 这些数据绑定控件不支持单个绑定字段的 ShowHeader 属性。These data-bound controls do not support the ShowHeader property for an individual bound field. 若要显示或隐藏数据绑定控件的整个标头部分,请使用控件的 ShowHeader 属性(如果可用)。To show or hide the entire header section of a data-bound control, use the control's ShowHeader property (if available).

还可以通过设置字段的不同部分的样式属性来自定义 HyperLinkField 对象(字体颜色、背景色等)的外观。You also can customize the appearance of the HyperLinkField object (font color, background color, and so on) by setting the style properties for the different parts of the field. 下表列出了不同的样式属性。The following table lists the different style properties.

Style 属性Style property 说明Description
ControlStyle HyperLinkField 对象的子 Web 服务器控件的样式设置。The style settings for the child Web server controls of the HyperLinkField object.
FooterStyle HyperLinkField 对象的脚注部分的样式设置。The style settings for the footer section of the HyperLinkField object.
HeaderStyle HyperLinkField 对象的标头部分的样式设置。The style settings for the header section of the HyperLinkField object.
ItemStyle HyperLinkField 对象中数据项的样式设置。The style settings for the data items in the HyperLinkField object.

构造函数

HyperLinkField()

初始化 HyperLinkField 类的新实例。Initializes a new instance of the HyperLinkField class.

属性

AccessibleHeaderText

获取或设置某些控件中呈现为 AbbreviatedText 属性值的文本。Gets or sets text that is rendered as the AbbreviatedText property value in some controls.

(继承自 DataControlField)
Control

获取对数据控件的引用,该控件与 DataControlField 对象关联。Gets a reference to the data control that the DataControlField object is associated with.

(继承自 DataControlField)
ControlStyle

获取 DataControlField 对象所包含的任何 Web 服务器控件的样式。Gets the style of any Web server controls contained by the DataControlField object.

(继承自 DataControlField)
DataNavigateUrlFields

获取或设置数据源中字段的名称,用于为 HyperLinkField 对象中的超链接构造 URL。Gets or sets the names of the fields from the data source used to construct the URLs for the hyperlinks in the HyperLinkField object.

DataNavigateUrlFormatString

获取或设置用于指定格式的字符串,HyperLinkField 对象中的超链接的 URL 将以此格式呈现。Gets or sets the string that specifies the format in which the URLs for the hyperlinks in a HyperLinkField object are rendered.

DataTextField

获取或设置数据源中的字段的名称,此名称包含要为 HyperLinkField 对象中的超链接标题显示的文本。Gets or sets the name of the field from the data source containing the text to display for the hyperlink captions in the HyperLinkField object.

DataTextFormatString

获取或设置用于指定格式的字符串,HyperLinkField 对象中的超链接标题将以此格式显示。Get or sets the string that specifies the format in which the hyperlink captions in a HyperLinkField object are displayed.

DesignMode

获取一个值,该值指示数据控件字段当前是否在设计时环境中进行查看。Gets a value indicating whether a data control field is currently viewed in a design-time environment.

(继承自 DataControlField)
FooterStyle

获取或设置数据控件字段脚注的样式。Gets or sets the style of the footer of the data control field.

(继承自 DataControlField)
FooterText

获取或设置数据控件字段的脚注项中显示的文本。Gets or sets the text that is displayed in the footer item of a data control field.

(继承自 DataControlField)
HeaderImageUrl

获取或设置数据控件字段的标题项中显示的图像的 URL。Gets or sets the URL of an image that is displayed in the header item of a data control field.

(继承自 DataControlField)
HeaderStyle

获取或设置数据控件字段标头的样式。Gets or sets the style of the header of the data control field.

(继承自 DataControlField)
HeaderText

获取或设置数据控件字段的标题项中显示的文本。Gets or sets the text that is displayed in the header item of a data control field.

(继承自 DataControlField)
InsertVisible

获取一个值,该值指示 DataControlField 对象在其父级数据绑定控件处于插入模式时是否可见。Gets a value indicating whether the DataControlField object is visible when its parent data-bound control is in insert mode.

(继承自 DataControlField)
IsTrackingViewState

获取一个值,该值指示 DataControlField 对象是否保存对其视图状态的更改。Gets a value indicating whether the DataControlField object is saving changes to its view state.

(继承自 DataControlField)
ItemStyle

获取由数据控件字段显示的任何基于文本的内容的样式。Gets the style of any text-based content displayed by a data control field.

(继承自 DataControlField)
NavigateUrl

获取或设置当单击 HyperLinkField 对象中的超链接时定位到的 URL。Gets or sets the URL to navigate to when a hyperlink in a HyperLinkField object is clicked.

ShowHeader

获取或设置一个值,该值指示是否呈现数据控件字段的标题项。Gets or sets a value indicating whether the header item of a data control field is rendered.

(继承自 DataControlField)
SortExpression

获取或设置数据源控件用来对数据进行排序的排序表达式。Gets or sets a sort expression that is used by a data source control to sort data.

(继承自 DataControlField)
Target

获取或设置目标窗口或框架,单击 HyperLinkField 对象中的超链接时,将在此显示链接到的网页。Gets or sets the target window or frame in which to display the Web page linked to when a hyperlink in a HyperLinkField object is clicked.

Text

获取或设置要为 HyperLinkField 对象中的每个超链接显示的文本。Gets or sets the text to display for each hyperlink in the HyperLinkField object.

ValidateRequestMode

获取或设置一个值,该值指定该控件是否验证客户端输入。Gets or sets a value that specifies whether the control validates client input.

(继承自 DataControlField)
ViewState

获取状态信息的字典,这些信息使您可以在同一页的多个请求间保存和还原 DataControlField 对象的视图状态。Gets a dictionary of state information that allows you to save and restore the view state of a DataControlField object across multiple requests for the same page.

(继承自 DataControlField)
Visible

获取或设置指示是否呈现数据控件字段的值。Gets or sets a value indicating whether a data control field is rendered.

(继承自 DataControlField)

方法

CloneField()

创建当前 DataControlField 派生对象的副本。Creates a duplicate copy of the current DataControlField-derived object.

(继承自 DataControlField)
CopyProperties(DataControlField)

将当前 HyperLinkField 对象的属性复制到指定对象。Copies the properties of the current HyperLinkField object to the specified object.

CreateField()

返回 HyperLinkField 类的新实例。Returns a new instance of the HyperLinkField class.

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
ExtractValuesFromCell(IOrderedDictionary, DataControlFieldCell, DataControlRowState, Boolean)

从当前表格单元格中提取数据控件字段的值,并将该值添加到指定的 IDictionary 集合中。Extracts the value of the data control field from the current table cell and adds the value to the specified IDictionary collection.

(继承自 DataControlField)
FormatDataNavigateUrlValue(Object[])

使用由 DataNavigateUrlFormatString 属性指定的格式字符串格式化导航 URL。Formats the navigation URL using the format string specified by the DataNavigateUrlFormatString property.

FormatDataTextValue(Object)

使用由 DataTextFormatString 属性指定的格式字符串格式化标题文本。Formats the caption text using the format string specified by the DataTextFormatString property.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
Initialize(Boolean, Control)

初始化 HyperLinkField 对象。Initializes the HyperLinkField object.

InitializeCell(DataControlFieldCell, DataControlCellType, DataControlRowState, Int32)

初始化 HyperLinkField 对象中的单元格。Initializes a cell in a HyperLinkField object.

LoadViewState(Object)

将数据源视图还原为保存过的前一视图状态。Restores the data source view's previously saved view state.

(继承自 DataControlField)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
OnFieldChanged()

引发 FieldChanged 事件。Raises the FieldChanged event.

(继承自 DataControlField)
SaveViewState()

保存在页回发到服务器后对 DataControlField 视图状态所做的更改。Saves the changes made to the DataControlField view state since the time the page was posted back to the server.

(继承自 DataControlField)
ToString()

返回表示此 DataControlField 对象的字符串。Returns a string that represents this DataControlField object.

(继承自 DataControlField)
TrackViewState()

使 DataControlField 对象跟踪对其视图状态所做的更改,以便这些更改可以存储在控件的 ViewState 属性中并且能够在同一页的不同请求间得以保持。Causes the DataControlField object to track changes to its view state so they can be stored in the control's ViewState property and persisted across requests for the same page.

(继承自 DataControlField)
ValidateSupportsCallback()

指示 HyperLinkField 对象中包含的控件支持回调。Indicates that the controls contained by the HyperLinkField object support callbacks.

显式接口实现

IDataSourceViewSchemaAccessor.DataSourceViewSchema

获取或设置与此 DataControlField 对象关联的架构。Gets or sets the schema associated with this DataControlField object.

(继承自 DataControlField)
IStateManager.IsTrackingViewState

获取一个值,该值指示 DataControlField 对象是否保存对其视图状态的更改。Gets a value indicating whether the DataControlField object is saving changes to its view state.

(继承自 DataControlField)
IStateManager.LoadViewState(Object)

将数据控件字段还原为保存过的前一视图状态。Restores the data control field's previously saved view state.

(继承自 DataControlField)
IStateManager.SaveViewState()

保存在页回发到服务器后对 DataControlField 视图状态所做的更改。Saves the changes made to the DataControlField view state since the time the page was posted back to the server.

(继承自 DataControlField)
IStateManager.TrackViewState()

使 DataControlField 对象跟踪对其视图状态所做的更改,以便这些更改可以存储在控件的 ViewState 属性中并且能够在同一页的不同请求间得以保持。Causes the DataControlField object to track changes to its view state so they can be stored in the control's ViewState property and persisted across requests for the same page.

(继承自 DataControlField)

适用于

另请参阅