HyperLinkField クラス
アセンブリ: System.Web (system.web.dll 内)
構文
解説
データ バインド コントロール (GridView、DetailsView など) はこの HyperLinkField クラスを使用して、表示される各レコードのハイパーリンクを表示します。ハイパーリンクがクリックされると、そのハイパーリンクに関連付けられた Web ページにユーザーをリダイレクトします。HyperLinkField オブジェクトは、そのオブジェクトを使用しているデータ バインド コントロールにより異なった形式で表示されます。たとえば、HyperLinkField オブジェクトは、GridView コントロールでは列として表示され、DetailsView コントロールでは行として表示されます。
ハイパーリンクに表示するキャプションを指定するには、Text プロパティを使用します。NavigateUrl プロパティを使用して、ハイパーリンクがクリックされたときの移動先となる URL を指定します。リンク先の内容を特定のウィンドウまたはフレームで表示させるには、Target プロパティを設定します。
メモ |
---|
Text プロパティおよび NavigateUrl プロパティを設定した場合、HyperLinkField オブジェクト内のすべてのハイパーリンクで同じキャプションとナビゲーション URL が共有されます。同様に、Target プロパティもすべてのハイパーリンクに適用されます。 |
HyperLinkField オブジェクトをデータ ソースのフィールドにバインドすることでも同じ設定を行うことができます。これにより、HyperLinkField オブジェクトのハイパーリンクごとに異なるキャプションを表示したり、個々のハイパーリンクの移動先に別々の場所を割り当てたりできます。フィールドをキャプションにバインドするには、DataTextField プロパティを設定します。ナビゲーション用の URL を作成するには、DataNavigateUrlFields プロパティに対し、URL の作成に使用する各フィールドをコンマ区切りで指定します。
キャプションおよびナビゲーション URL に対してカスタム書式を指定するには、それぞれ、DataTextFormatString プロパティと DataNavigateUrlFormatString プロパティを設定します。
Visible プロパティを false に設定すると、データ バインド コントロールの HyperLinkField オブジェクトを非表示にできます。
また、HyperLinkField オブジェクトのヘッダー セクションおよびフッター セクションをカスタマイズできます。ヘッダー セクションまたはフッター セクションのキャプションを表示するには、HeaderText または FooterText プロパティを設定します。ヘッダー セクションにテキストではなくイメージを表示するには、HeaderImageUrl プロパティを設定します。ShowHeader プロパティに false を設定すると、HyperLinkField オブジェクトでヘッダー セクションを非表示にできます。
メモ |
---|
一部のデータ バインド コントロール (GridView コントロールなど) では、コントロールのヘッダー セクションをすべて表示するか、すべて非表示にするかしかできません。これらのデータ バインド コントロールは、個々のバインド フィールドの ShowHeader プロパティはサポートしていません。データ バインド コントロールのヘッダー セクション (存在する場合) 全体を表示または非表示にするには、コントロールの ShowHeader プロパティを使用します。 |
また、フィールドの各部分にスタイル プロパティを設定すると、HyperLinkField オブジェクトの外観 (フォントの色や背景色など) をカスタマイズできます。さまざまなスタイル プロパティの一覧を次の表に示します。
HyperLinkField オブジェクトを使用して、GridView コントロールに静的ハイパーリンクの列を表示する方法を次のコード例に示します。HyperLinkField オブジェクト内の各ハイパーリンクは、Text プロパティと NavigateUrl プロパティで指定された同じキャプションおよびナビゲーション URL を共有します。
<%@ Page language="VB" %> <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>
<%@ 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="VB" %> <html> <body> <form 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>
<%@ 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>
System.Web.UI.WebControls.DataControlField
System.Web.UI.WebControls.HyperLinkField
プラットフォーム
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
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
HyperLinkField メンバ
System.Web.UI.WebControls 名前空間
GridView クラス
GridView.Columns プロパティ
GridView.ShowHeader プロパティ
DetailsView クラス
DetailsView.Fields プロパティ
BoundField クラス
ButtonField クラス
ButtonFieldBase クラス
CheckBoxField クラス
CommandField クラス
DataControlField クラス
TemplateField
DataNavigateUrlFields
DataNavigateUrlFormatString
DataTextField
DataTextFormatString
NavigateUrl
Target
Text
DataControlField.Visible プロパティ
- HyperLinkField クラスのページへのリンク