HtmlImage 服务器控件声明性语法
[本文档仅供预览,在以后的发行版中可能会发生更改。包含的空白主题用作占位符。]
创建服务器端控件,该控件映射到 <img> HTML 元素并允许您显示图像。
<img
EnableViewState="False|True"
Id="string"
Visible="False|True"
OnDataBinding="OnDataBinding event handler"
OnDisposed="OnDisposed event handler"
OnInit="OnInit event handler"
OnLoad="OnLoad event handler"
OnPreRender="OnPreRender event handler"
OnUnload="OnUnload event handler"
runat="server"
/>
备注
使用 HtmlImage 控件对 HTML <img> 元素进行编程。 此控件允许您分别使用 Src、Width、Height、Border、Alt 和 Align 属性,动态设置和检索图像的源、宽度、高度、边框宽度、替换文本和对齐方式。
备注
此控件不需要结束标记。
示例
下面的示例显示如何基于用户选择更改显示的图像。 它将 /images 目录指定为要显示的图像的源路径。 在 Web 窗体页的 HtmlSelect 控件中选定的值确定要显示 /images 目录中的哪个图像。
<%@ Page Language="VB" AutoEventWireup="True" %>
<!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>
<title>HtmlImage Control</title>
<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 id="Form1" runat="server">
<img id="Image1" src="/images/cereal1.gif" runat="server" alt="Cereal bowl"/>
<br />
<br />
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>
<br />
<br />
<input id="Submit1" type="submit" runat="server" value="Apply"
onserverclick="SubmitBtn_Click" />
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>
<!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>
<title>HtmlImage Control</title>
<script runat="server">
void SubmitBtn_Click(Object Sender, EventArgs e)
{
Image1.Src="/images/" + Select1.Value;
}
</script>
</head>
<body>
<h3>HtmlImage Sample</h3>
<form id="Form1" runat="server">
<img id="Image1" src="/images/cereal1.gif" runat="server" alt="Cereal bowl"/>
<br />
<br />
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>
<br />
<br />
<input id="Submit1" type="submit" runat="server" value="Apply"
onserverclick="SubmitBtn_Click" />
</form>
</body>
</html>