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 Form 網頁 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>