다음을 통해 공유


HyperLinkField 생성자

정의

HyperLinkField 클래스의 새 인스턴스를 초기화합니다.

public:
 HyperLinkField();
public HyperLinkField ();
Public Sub New ()

예제

다음 코드 예제에는 동적으로 추가 하는 생성자를 사용 하는 방법을 보여 줍니다.는 HyperLinkField 개체는 GridView 제어 합니다.


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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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 xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>HyperLinkField Constructor Example</title>
</head>
<body>
    <form id="form1" 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="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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 xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>HyperLinkField Constructor Example</title>
</head>
<body>
    <form id="form1" 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>

설명

이 생성자의 새 인스턴스를 초기화 하는 데는 HyperLinkField 클래스입니다. 이 생성자는 필드를 동적으로 생성된 하는 데이터 바인딩된 컨트롤을 추가할 때 일반적으로 사용 됩니다.

동적으로 추가 하는 HyperLinkField 데이터 바인딩된 컨트롤에 개체를 새 HyperLinkField 개체 속성을 설정 하 고 다음 데이터 바인딩된 컨트롤의 필드 컬렉션에 추가 합니다. 예를 들어, 사용 중인 경우는 GridView 컨트롤을 추가 합니다 HyperLinkField 개체를 Columns 컬렉션.

참고

데이터 바인딩된 컨트롤에 필드를 동적으로 추가할 수 있습니다, 있지만 것이 좋습니다는 필드 정적으로 선언 하 고 다음 표시 하거나 숨길 수를 적절 하 게 합니다. 모든 필드를 정적으로 선언 된 부모 데이터 바인딩된 컨트롤에 대 한 뷰 상태의 크기를 줄입니다.

적용 대상

추가 정보