HyperLinkField コンストラクタ
アセンブリ: System.Web (system.web.dll 内)


このコンストラクタを使用して、HyperLinkField クラスの新しいインスタンスを初期化します。通常、このコンストラクタは、動的に作成されたデータ バインド コントロールにフィールドを追加する場合に使用されます。
HyperLinkField オブジェクトをデータ バインド コントロールに動的に追加するには、新しい HyperLinkField オブジェクトを作成して、プロパティを設定してから、そのオブジェクトをデータ バインド コントロールのフィールド コレクションに追加します。たとえば、GridView コントロールを使用する場合は、HyperLinkField オブジェクトを Columns コレクションに追加します。
![]() |
---|
フィールドはデータ バインド コントロールに動的に追加することもできますが、フィールドを静的に宣言しておき、適宜、表示または非表示にすることをお勧めします。フィールドをすべて静的に宣言すると、親データ バインド コントロールのビューステートのサイズを削減できます。 |

次のコード例では、コンストラクタを使用して、HyperLinkField オブジェクトを GridView コントロールに動的に追加する方法を示します。
<%@ Page language="VB" %> <script runat="server"> Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) ' Dynamically create field columns to display the desired ' fields from the data source. This only needs to be done ' the first time the page is loaded because the GridView ' control persists its column fields. If Not IsPostBack Then ' Create a HyperLinkField object to display the company's ' name. Bind the CompanyName and HomePage fields from the ' Northwind database to the caption and URL of the hyperlinks ' in the HyperLinkField field column. Note that the URLs ' specified in the Northwind database might not be valid URLs. Dim companyNameBoundField As New HyperLinkField Dim dataNavigateUrlFields() As String = {"HomePage"} companyNameBoundField.DataTextField = "CompanyName" companyNameBoundField.DataNavigateUrlFields = dataNavigateUrlFields companyNameBoundField.HeaderText = "Company Name" companyNameBoundField.Target = "_blank" ' Create a BoundField object to display the company's city. Dim cityBoundField As New BoundField cityBoundField.DataField = "city" cityBoundField.HeaderText = "City" ' Add the field columns to the Columns collection of the ' GridView control. SuppliersGridView.Columns.Add(companyNameBoundField) SuppliersGridView.Columns.Add(cityBoundField) End If End Sub </script> <html> <body> <form runat="server"> <h3>HyperLinkField Constructor Example</h3> <asp:gridview id="SuppliersGridView" datasourceid="SuppliersSqlDataSource" autogeneratecolumns="False" runat="server"> </asp:gridview> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. --> <asp:sqldatasource id="SuppliersSqlDataSource" selectcommand="SELECT [SupplierID], [CompanyName], [City], [HomePage] FROM [Suppliers]" connectionstring="server=localhost;database=northwind;integrated security=SSPI" runat="server"> </asp:sqldatasource> </form> </body> </html>
<%@ Page language="C#" %> <script runat="server"> void Page_Load(Object sender, EventArgs e) { // Dynamically create field columns to display the desired // fields from the data source. This only needs to be done // the first time the page is loaded because the GridView // control persists its column fields. if (!IsPostBack) { // Create a HyperLinkField object to display the company's // name. Bind the CompanyName and HomePage fields from the // Northwind database to the caption and URL of the hyperlinks // in the HyperLinkField field column. Note that the URLs // specified in the Northwind database might not be valid URLs. HyperLinkField companyNameBoundField = new HyperLinkField (); string[] dataNavigateUrlFields = { "HomePage" }; companyNameBoundField.DataTextField = "CompanyName"; companyNameBoundField.DataNavigateUrlFields = dataNavigateUrlFields; companyNameBoundField.HeaderText = "Company Name"; companyNameBoundField.Target = "_blank"; // Create a BoundField object to display the company's city. BoundField cityBoundField = new BoundField (); cityBoundField.DataField = "city"; cityBoundField.HeaderText = "City"; // Add the field columns to the Columns collection of the // GridView control. SuppliersGridView.Columns.Add (companyNameBoundField); SuppliersGridView.Columns.Add (cityBoundField); } } </script> <html> <body> <form runat="server"> <h3>HyperLinkField Constructor Example</h3> <asp:gridview id="SuppliersGridView" datasourceid="SuppliersSqlDataSource" autogeneratecolumns="False" runat="server"> </asp:gridview> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. --> <asp:sqldatasource id="SuppliersSqlDataSource" selectcommand="SELECT [SupplierID], [CompanyName], [City], [HomePage] FROM [Suppliers]" connectionstring="server=localhost;database=northwind;integrated security=SSPI" runat="server"> </asp:sqldatasource> </form> </body> </html>

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 コンストラクタのページへのリンク