共用方式為


ImageField 建構函式

定義

初始化 ImageField 類別的新執行個體。

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

範例

下列範例示範如何使用這個建構函式,以動態方式將 物件加入 ImageField 控制項 ColumnsGridView 集合。


<%@ 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 集合。

注意

雖然您可以動態將欄位新增至資料繫結控制項,但強烈建議以靜態方式宣告欄位,然後視需要顯示或隱藏欄位。 以靜態方式宣告所有欄位會減少父資料繫結控制項的檢視狀態大小。

適用於

另請參閱