Home > Getting Started > ASP.NET Syntax > ASP.NET Syntax for HTML Controls > HtmlImage
ASP.NET Syntax ASP.NET Syntax for HTML Controls
Embeds an image control in a Web Forms document.
Declarative Syntax
<img id="accessID" runat="server"
alt = "alttext"
align= top | middle | bottom | left | right
border = "borderwidth"
height = "imageheight"
src = "imageURI"
width = "imagewidth"
>
For information on the individual members of this class, see HtmlImage in the class library.
- This control does not require a closing tag.
The HtmlImage server control renders an image file specified by it’s Src property.
The HtmlImage control enables programming of the HTML <img
> element, enabling developers to dynamically set and retrieve image attributes, including the image file’s src, width, height, border, alt, and align attributes.
This sample illustrates use of the HtmlImage control.
The following example shows how to dynamically change a displayed image in a Web Forms page based on what the user chooses. An HtmlSelect control provides the list of user options, in which the value selected determines the image to display.
The sample uses the click event of the HtmlInputButton control to trigger a handler that simply specifies the source path of the image file to be displayed, in this case, from the application’s /images directory.
<html>
<head>
<script language="C#" runat="server">
void submitHandler ( Object Sender, EventArgs e ) {
Image1.Src = "images/" + Select1.Value;
}
</script>
</head>
<body>
<form runat="server">
<img id="Image1" src = "images/cereal1.gif" runat="server" />
<p>Select image file: </p>
<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>
<input type = "submit" runat="server" value="Apply"
onServerClick = "submitHandler">
</form>
</body>
</html>
Web Forms Events and Handlers HtmlImage Class