Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
NullImageUrl Property

ImageField.NullImageUrl Property

Gets or sets the URL to an alternate image displayed in an ImageField object when the value of the field specified by the DataImageUrlField property is null.

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

public virtual string NullImageUrl { get; set; }

Property Value

Type: System.String
The URL to an alternate image displayed when the value of a field is null. The default is an empty string (""), which indicates that this property is not set.

When the value of the field specified by the DataImageUrlField property is null, an image cannot be displayed in an ImageField object. Use the NullImageUrl property to specify the URL to an alternate image to display. The alternate image is usually an image that indicates that the normal image is not available or cannot be found.

NoteNote

As an alternative, you can simply display text when the field value is null by setting the NullDisplayText property instead of this property. If the NullImageUrl and NullDisplayText properties are both set, the NullImageUrl property has precedence.

The following example demonstrates how to use the NullImageUrl property to specify the URL to an alternate image to display when the value of the field specified by the DataImageUrlField property is null.


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

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

      <h3>ImageField Example</h3>

      <asp:gridview id="EmployeesGrid"
        autogeneratecolumns="false"
        datasourceid="EmployeeSource"  
        runat="server">

        <columns>

          <asp:imagefield dataimageurlfield="PhotoPath"
            alternatetext="Employee Photo"
            nullimageurl="~\Images\NoPhoto.jpg"
            headertext="Photo"  
            readonly="true"/>
          <asp:boundfield datafield="FirstName"
            headertext="First Name"/>
          <asp:boundfield datafield="LastName"
            headertext="Last Name"/>

        </columns>

      </asp:gridview>

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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0
Show:
© 2015 Microsoft