HtmlImage Control

Creates a server-side control that maps to the <img> HTML element and allows you to display an image.

<img id="programmaticID"
     alt="alttext"
     align= top | middle | bottom | left | right
     border="borderwidth"
     height="imageheight"
     src="imageURL"
     width="imagewidth"   
     runat="server" >

Remarks

Use the HtmlImage control to program against the HTML <img> element. This control allows you to dynamically set and retrieve the image's source, width, height, border width, alternate text, and alignment by using the Src, Width, Height, Border, Alt, and Align properties, respectively.

Note   This control does not require a closing tag.

Example

The following example shows how to change a displayed image, based on user choices. It specifies the /images directory as the source path for the images to display. The value selected in the HtmlSelect control in the Web Forms page determines which image to display from the /images directory.

<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
<head>
   <script runat="server">
      Sub SubmitBtn_Click(Sender As Object, e As EventArgs)
         Image1.Src = "/images/" & Select1.Value
      End Sub
   </script>
</head>

<body>
   <h3>HtmlImage Sample</h3>
   <form runat="server">
      <img ID="Image1" src="/images/cereal1.gif" runat="server"/>
      <p>
      <p>
      Select an image file to display:
      <select id="Select1" runat="server">
         <option Value="Cereal1.gif">Healthy Grains</option>
         <option Value="Cereal2.gif">Corn Flake Cereal</option>
         <option Value="Cereal3.gif">U.F.O.S</option>
         <option Value="Cereal4.gif">Oatey O's</option>
         <option Value="Cereal5.gif">Strike</option>
         <option Value="Cereal7.gif">Fruity Pops</option>
       </select>
       <p>
       <p>
       <input type="submit" runat="server" Value="Apply"
              OnServerClick="SubmitBtn_Click">
   </form>
</body>
</html>
[C#]
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
    <script runat="server">
       void SubmitBtn_Click(Object Sender, EventArgs e) 
       {
          Image1.Src="/images/" + Select1.Value;
       }
    </script>
</head>

<body>
   <h3>HtmlImage Sample</h3>
   <form runat="server">
      <img ID="Image1" src="/images/cereal1.gif" runat="server"/>
      <p>
      <p>
      Select an image file to display:
      <select id="Select1" runat="server">
         <option Value="Cereal1.gif">Healthy Grains</option>
         <option Value="Cereal2.gif">Corn Flake Cereal</option>
         <option Value="Cereal3.gif">U.F.O.S</option>
         <option Value="Cereal4.gif">Oatey O's</option>
         <option Value="Cereal5.gif">Strike</option>
         <option Value="Cereal7.gif">Fruity Pops</option>
       </select>
       <p>
       <p>
       <input type="submit" runat="server" Value="Apply"
              OnServerClick="SubmitBtn_Click">
   </form>
</body>
</html>

See Also

ASP.NET Syntax for HTML Controls | HtmlImage Class