ImageField.AlternateText 属性

定义

获取或设置为 ImageField 对象中的图像显示的备用文本。

public:
 virtual property System::String ^ AlternateText { System::String ^ get(); void set(System::String ^ value); };
public virtual string AlternateText { get; set; }
member this.AlternateText : string with get, set
Public Overridable Property AlternateText As String

属性值

显示在 ImageField 对象中的图像的备用文本。 默认值为空字符串 (""),表示尚未设置此属性。

示例

以下示例演示如何使用 AlternateText 属性为对象中显示的 ImageField 所有图像指定替换文本。


<%@ 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"
            nulldisplaytext="No image on file."
            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>

<%@ Page language="VB" %>

<!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"
            nulldisplaytext="No image on file."
            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>

注解

AlternateText使用 属性可以指定对象中显示的ImageField图像的备用文本。 当图像无法加载或不可用时,将显示备用文本。 支持工具提示功能的浏览器还会将此文本显示为工具提示。

注意

设置此属性后,对象中显示的 ImageField 所有图像共享相同的备用文本。 或者,可以通过设置 DataAlternateTextField 属性而不是此属性,将备用文本绑定到数据源中的字段。 这样,你可针对显示的每个图像使用不同的备用文本。 如果同时 AlternateText 设置了 和 DataAlternateTextField 属性,则 DataAlternateTextField 属性优先。

设置此属性的值时,可以使用设计器工具自动保存到资源文件。 有关详细信息,请参阅 LocalizableAttribute全球化和本地化

适用于

另请参阅