asp.net.ph

Image Class

System.Web.UI.WebControls Namespace


Renders an image on a Web page.

Image Class Members

Collapse   Constructors

Visibility Constructor Parameters
public Image ( )

Collapse   Properties

Visibility Name Value Type Accessibility
public AlternateText String [ Get , Set ]
public DescriptionUrl String [ Get , Set ]
public Enabled Boolean [ Get , Set ]
public Font FontInfo [ Get ]
public GenerateEmptyAlternateText Boolean [ Get , Set ]
public ImageAlign ImageAlign [ Get , Set ]
public ImageUrl String [ Get , Set ]
public SupportsDisabledAttribute Boolean [ Get ]

Collapse   Methods

Visibility Name Parameters Return Type
protected AddAttributesToRender ( HtmlTextWriter writer ) Void

Remarks

Use the Image control to display an image on the Web page. The path to the displayed image is specified by setting the ImageUrl property. You can specify the text to display in place of the image when the image is not available by setting the AlternateText property. The alignment of the image in relation to other elements on the Web page is specified by setting ImageAlign property.

NOTE: If you need to capture mouse clicks on the image, use the ImageButton control.

Image is particularly useful for programmatically setting images at a specified location on the page at run time, in which the image file paths are determined dynamically, either from user input or from some other process in your code.

In certain cases, as when images are used within templated controls, the ImageUrl, or a part thereof, is usually obtained dynamically from a data source.

Example

Below is some code snippet from our sample storefront, which displays a variable "ratings" image based on customer reviews. The images are stored as stars-5.gif, stars-4.gif and so on. The image to display depends on the value of the rating field, which is derived dynamically for each item in the DataList at run time.

void getReviews ( Object src, DataListItemEventArgs e ) {
   if ( asinData.Tables.Contains ( "CustomerReview" ) ) {
      if ( e.Item.ItemType == ListItemType.Item ||
            e.Item.ItemType == ListItemType.AlternatingItem ) {

         // set reference to current data record
         DataRowView rowView = ( DataRowView ) e.Item.DataItem;

         // find panel wherein to add the image
         HtmlGenericControl rating = ( HtmlGenericControl ) e.Item.FindControl ( "rating" );

         // get customer ratings
         string strRating = rowView [ "rating" ].ToString ( );

         if ( strRating != "0" ) {
            Image img = new Image ( );
            img.ImageAlign = ImageAlign.AbsMiddle;
            img.ImageUrl = string.Format ( "images/stars-{0}.gif", strRating );
            rating.Controls.Add ( img );
         }
         // otherwise, hide ratings panel
         else rating.Visible = false;
         ...
      }
   }
}

 Show me 

For other examples illustrating use of this control, see the individual member types of this class. For syntax information, see Image in ASP.NET Syntax for Web Controls.

See Also

ImageButton Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph