ImageField クラス
アセンブリ: System.Web (system.web.dll 内)


データ バインド コントロール (GridView、DetailsView など) はこの ImageField クラスを使用して、表示される各レコードのイメージを表示します。ImageField オブジェクトは、そのオブジェクトを使用しているデータ バインド コントロールにより異なった形式で表示されます。たとえば、ImageField オブジェクトは、GridView コントロールでは列として表示され、DetailsView コントロールでは行として表示されます。
イメージを表示するには、ImageField オブジェクトを、イメージの URL が格納されているデータ ソースのフィールドにバインドする必要があります。これは、DataImageUrlField プロパティを設定することによって行います。URL 値は DataImageUrlFormatString プロパティを使って書式設定できます。イメージごとに、代替テキストを関連付けることもできます。イメージを読み込むことができなかったり、イメージにアクセスできなかった場合、このテキストが表示されます。ツール ヒント機能をサポートしているブラウザの場合、このテキストはツール ヒントとしても表示されます。表示されるイメージの代替テキストを指定するには、次のいずれかの方法を使用します。
-
データ ソースのフィールドを、各イメージの AlternateText プロパティにバインドするには、DataAlternateTextField プロパティを使用します。これにより、表示されるイメージごとに異なる代替テキストが割り当てられます。バインディング時には、DataAlternateTextFormatString プロパティを使用して、代替テキストの書式を設定することもできます (省略可能)。
イメージの URL 値が null 参照 (Visual Basic では Nothing) の場合、イメージは表示されません。null 参照 (Visual Basic では Nothing) のフィールド値に対して代替イメージを表示するには、NullImageUrl プロパティを設定します。代替イメージではなく、代替テキストが表示されるようにするには、NullDisplayText プロパティを設定します。
既定では、宣言したすべてのフィールドがデータ バインド コントロールに表示されます。Visible プロパティを false に設定すると、データ バインド コントロールの ImageField オブジェクトを非表示にできます。
また、ImageField オブジェクトのヘッダー セクションおよびフッター セクションをカスタマイズできます。ヘッダーまたはフッター セクションのキャプションを表示するには、HeaderText または FooterText プロパティをそれぞれ設定します。ヘッダー セクションにテキストではなくイメージを表示するには、HeaderImageUrl プロパティを設定します。ShowHeader プロパティに false を設定すると、ImageField オブジェクトでヘッダー セクションを非表示にできます。
![]() |
---|
一部のデータ バインド コントロール (GridView コントロールなど) では、コントロールのヘッダー セクションをすべて表示するか、すべて非表示にするかしかできません。これらのデータ バインド コントロールは、個々のデータ バインド フィールドの ShowHeader プロパティはサポートしていません。データ バインド コントロールのヘッダー セクション (存在する場合) 全体を表示または非表示にするには、コントロールの ShowHeader プロパティを使用します。 |
また、フィールドの各部分にスタイル プロパティを設定すると、ImageField オブジェクトの外観 (フォントの色や背景色など) をカスタマイズできます。さまざまなスタイル プロパティの一覧を次の表に示します。

GridView コントロールの ImageField オブジェクトを使用してイメージを表示する方法を次の例に示します。
<%@ Page language="VB" %> <html> <body> <form 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="C#" %> <html> <body> <form 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>


System.Web.UI.WebControls.DataControlField
System.Web.UI.WebControls.ImageField


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ImageField メンバ
System.Web.UI.WebControls 名前空間
GridView クラス
GridView.Columns プロパティ
DetailsView クラス
DetailsView.Fields プロパティ
BoundField クラス
ButtonField クラス
CheckBoxField クラス
CommandField クラス
HyperLinkField クラス
TemplateField
AlternateText
DataAlternateTextField
DataAlternateTextFormatString
DataImageUrlField
DataImageUrlFormatString
NullDisplayText
NullImageUrl
- ImageField クラスのページへのリンク