ImageField Constructor ()


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Initializes a new instance of the ImageField class.

Namespace:   System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public ImageField()

Use this constructor to initialize a new instance of the ImageField class. This constructor is commonly used when adding fields to a dynamically created data-bound control.

To dynamically add an ImageField object to a data-bound control, create a new ImageField object, set its properties, and then add it to the data-bound control's field collection. For example, if you are using the GridView control, add the ImageField object to the Columns collection.


Although you can dynamically add fields to a data-bound control, it is strongly recommended that fields be statically declared and then shown or hidden, as appropriate. Statically declaring all your fields reduces the size of the view state for the parent data-bound control.

The following example demonstrates how to use this constructor to dynamically add an ImageField object to the Columns collection of a GridView control.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<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.

      // Add the GridView control to the Controls collection
      // of the PlaceHolder control. 




<html xmlns="" >
  <head runat="server">
    <title>ImageField Constructor Example</title>
    <form id="form1" runat="server">

      <h3>ImageField Constructor Example</h3>

      <asp:placeholder id="GridViewPlaceHolder"

      <!-- 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%>" 


.NET Framework
Available since 2.0
Return to top