다음을 통해 공유


ImageField 생성자

정의

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

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

예제

다음 예제에서는이 생성자를 사용 하 여 동적으로 추가 하는 방법을 보여 줍니다는 ImageField 개체를 Columns 의 컬렉션을 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)
  {

    if (!IsPostBack)
    {
      
      // Dynamically create a GridView control.
      GridView employeesView = new GridView();
      employeesView.ID = "EmployeesGrid";
      employeesView.AutoGenerateColumns = false;
      employeesView.DataSourceID = "EmployeeSource";

      // Dynamically create field columns to display the desired
      // fields from the data source.

      // Create an ImageField object to display an employee's photo.
      ImageField photoImageField = new ImageField();
      photoImageField.DataImageUrlField = "PhotoPath";
      photoImageField.AlternateText = "Employee Photo";
      photoImageField.NullDisplayText = "No image on file.";
      photoImageField.HeaderText = "Photo";
      photoImageField.ReadOnly = true;

      // Create a BoundField object to display an employee's last name.
      BoundField lastNameBoundField = new BoundField();
      lastNameBoundField.DataField = "LastName";
      lastNameBoundField.HeaderText = "Last Name";

      // Create a BoundField object to display an employee's first name.
      BoundField firstNameBoundField = new BoundField();
      firstNameBoundField.DataField = "FirstName";
      firstNameBoundField.HeaderText = "First Name";

      // Add the field columns to the Fields collection of the
      // GridView control.
      employeesView.Columns.Add(photoImageField);
      employeesView.Columns.Add(lastNameBoundField);
      employeesView.Columns.Add(firstNameBoundField);
      
      // Add the GridView control to the Controls collection
      // of the PlaceHolder control. 
      GridViewPlaceHolder.Controls.Add(employeesView);

    }

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ImageField Constructor Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>ImageField Constructor Example</h3>
                  
      <asp:placeholder id="GridViewPlaceHolder"
        runat="server"/>            
           
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="EmployeeSource"
        selectcommand="Select [EmployeeID], [LastName], [FirstName], [PhotoPath] From [Employees]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </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)

    If Not IsPostBack Then
      
      ' Dynamically create a GridView control.
      Dim employeesView As GridView = New GridView()
      employeesView.ID = "EmployeesGrid"
      employeesView.AutoGenerateColumns = False
      employeesView.DataSourceID = "EmployeeSource"

      ' Dynamically create field columns to display the desired
      ' fields from the data source.

      ' Create an ImageField object to display an employee's photo.
      Dim photoImageField As ImageField = New ImageField()
      photoImageField.DataImageUrlField = "PhotoPath"
      photoImageField.AlternateText = "Employee Photo"
      photoImageField.NullDisplayText = "No image on file."
      photoImageField.HeaderText = "Photo"
      photoImageField.ReadOnly = True

      ' Create a BoundField object to display an employee's last name.
      Dim lastNameBoundField As BoundField = New BoundField()
      lastNameBoundField.DataField = "LastName"
      lastNameBoundField.HeaderText = "Last Name"

      ' Create a BoundField object to display an employee's first name.
      Dim firstNameBoundField As BoundField = New BoundField()
      firstNameBoundField.DataField = "FirstName"
      firstNameBoundField.HeaderText = "First Name"

      ' Add the field columns to the Fields collection of the
      ' GridView control.
      employeesView.Columns.Add(photoImageField)
      employeesView.Columns.Add(lastNameBoundField)
      employeesView.Columns.Add(firstNameBoundField)
      
      ' Add the GridView control to the Controls collection
      ' of the PlaceHolder control. 
      GridViewPlaceHolder.Controls.Add(employeesView)

    End If

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ImageField Constructor Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>ImageField Constructor Example</h3>
                  
      <asp:placeholder id="GridViewPlaceHolder"
        runat="server"/>            
           
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="EmployeeSource"
        selectcommand="Select [EmployeeID], [LastName], [FirstName], [PhotoPath] From [Employees]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </form>
  </body>
</html>

설명

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

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

참고

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

적용 대상

추가 정보