HyperLinkField 类
表示在数据绑定控件中显示为超链接的字段。
数据绑定控件(如 GridView 和 DetailsView)使用 HyperLinkField 类,为每个已显示的记录显示超链接。当用户单击超链接时,此人将会被定向到与此超链接相关联的网页。根据在其中使用 HyperLinkField 对象的数据绑定控件,该对象会以不同的方式显示。例如,GridView 控件将 HyperLinkField 对象显示为一列,而 DetailsView 控件则将该对象显示为一行。
若要指定要为超链接显示的标题,请使用 Text 属性。使用 NavigateUrl 属性指定单击超链接时定位到的 URL。如果要在特定的窗口或框架中显示链接的内容,请设置 Target 属性。
注意 |
|---|
|
设置完 Text 和 NavigateUrl 属性后,HyperLinkField 对象中的所有超链接都将共享同一标题和导航 URL。同样,Target 属性也适用于所有超链接。 |
此外,您还可以将 HyperLinkField 对象绑定到数据源中的字段。这使您可以为 HyperLinkField 对象中的每个超链接显示不同的标题,并可以使每个超链接定位到不同位置。若要将字段绑定到标题,请设置 DataTextField 属性。若要创建用于导航的 URL,请将 DataNavigateUrlFields 属性设置为以逗号分隔的列表,此列表列出了用于创建 URL 的字段。 通过分别设置 DataTextFormatString 和 DataNavigateUrlFormatString 属性,可以为标题和导航 URL 指定自定义的格式。
通过将 Visible 属性设置为 false,可以在数据绑定控件中隐藏 HyperLinkField 对象。
可以自定义 HyperLinkField 对象的标头和脚注部分。若要在标头或脚注部分显示标题,请分别设置 HeaderText 或 FooterText 属性。若要在标头部分显示图像而不是文本,请设置 HeaderImageUrl 属性。通过将 ShowHeader 属性设置为 false,可以将标头部分隐藏在 HyperLinkField 对象中。
注意 |
|---|
|
某些数据绑定控件(如 GridView 控件)只能显示或隐藏控件的整个标头部分。这些数据绑定控件不支持单个绑定字段的 ShowHeader 属性。若要显示或隐藏数据绑定控件的整个标头部分,请使用该控件的 ShowHeader 属性(如果可用)。 |
您还可以通过为字段的不同部件设置样式属性来自定义 HyperLinkField 对象的外观(字体颜色、背景颜色等)。下表列出了不同的样式属性。
|
样式属性 |
说明 |
|---|---|
|
HyperLinkField 对象的 Web 服务器子控件的样式设置。 | |
|
HyperLinkField 对象的脚注部分的样式设置。 | |
|
HyperLinkField 对象的标头部分的样式设置。 | |
|
HyperLinkField 对象中数据项的样式设置。 |
示例 下面的代码示例演示如何使用 HyperLinkField 对象在 GridView 控件中显示静态超链接的列。HyperLinkField 对象中的每个超链接共享分别通过 Text 和 NavigateUrl 属性指定的同一标题和导航 URL。
<%@ Page language="C#" %>
<html>
<body>
<form 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 对象绑定到数据源中的字段。DataTextField 和 DataNavigateUrlFields 属性用于指定字段,这些字段分别要绑定到 HyperLinkField 对象中显示的每个超链接的标题和导航 URL。
<%@ Page language="C#" %>
<html>
<body>
<form 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>

注意
浙公网安备 33010602011771号