ASP.NET Tutorial/ASP.net Controls/Image
Содержание
Image alignment
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default_aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Image Control</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Image Control</h1>
<asp:Image ID="img1" runat="server" AlternateText="Dan" ImageUrl="http://www.nfex.ru/style/logo.png" />
<hr />
<asp:Button runat="server" Text="Sample Button" />
<asp:Image ID="img2" runat="server" AlternateText="Dan" ImageUrl="http://www.nfex.ru/style/logo.png" />
<hr />
<asp:DropDownList ID="ddl" runat="server" AutoPostBack="True">
<asp:ListItem text="NotSet" />
<asp:ListItem text="AbsBottom" />
<asp:ListItem text="AbsMiddle" />
<asp:ListItem text="Top" />
<asp:ListItem text="Bottom" />
<asp:ListItem text="BaseLine" />
<asp:ListItem text="TextTop" />
<asp:ListItem text="Left" />
<asp:ListItem text="Right" />
</asp:DropDownList>
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default_aspx : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
switch (ddl.SelectedIndex)
{
case 0:
img1.ImageAlign = ImageAlign.NotSet;
img2.ImageAlign = ImageAlign.NotSet;
break;
case 1:
img1.ImageAlign = ImageAlign.AbsBottom;
img2.ImageAlign = ImageAlign.AbsBottom;
break;
case 2:
img1.ImageAlign = ImageAlign.AbsMiddle;
img2.ImageAlign = ImageAlign.AbsMiddle;
break;
case 3:
img1.ImageAlign = ImageAlign.Top;
img2.ImageAlign = ImageAlign.Top;
break;
case 4:
img1.ImageAlign = ImageAlign.Bottom;
img2.ImageAlign = ImageAlign.Bottom;
break;
case 5:
img1.ImageAlign = ImageAlign.Baseline;
img2.ImageAlign = ImageAlign.Baseline;
break;
case 6:
img1.ImageAlign = ImageAlign.Middle;
img2.ImageAlign = ImageAlign.Middle;
break;
case 7:
img1.ImageAlign = ImageAlign.TextTop;
img2.ImageAlign = ImageAlign.TextTop;
break;
case 8:
img1.ImageAlign = ImageAlign.Left;
img2.ImageAlign = ImageAlign.Left;
break;
case 9:
img1.ImageAlign = ImageAlign.Right;
img2.ImageAlign = ImageAlign.Right;
break;
default:
img1.ImageAlign = ImageAlign.NotSet;
img2.ImageAlign = ImageAlign.NotSet;
break;
}
}
}
Important properties of Image control
AlternateText: alternate text for the image required for accessibility.
DescriptionUrl: a link to a page that contains a detailed description of the image (required to make a complex image accessible).
GenerateEmptyAlternateText: set the AlternateText property to an empty string.
ImageAlign: align the image relative to other HTML elements in the page. Possible values are AbsBottom, AbsMiddle, Baseline, Bottom, Left, Middle, NotSet, Right, TextTop, and Top.
ImageUrl: specify the URL to the image.
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Load()
{
Random rnd = new Random();
switch (rnd.Next(3))
{
case 0:
imgRandom.ImageUrl = "Picture1.gif";
imgRandom.AlternateText = "Picture 1";
break;
case 1:
imgRandom.ImageUrl = "Picture2.gif";
imgRandom.AlternateText = "Picture 2";
break;
case 2:
imgRandom.ImageUrl = "Picture3.gif";
imgRandom.AlternateText = "Picture 3";
break;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Show Image</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image
id="imgRandom"
Runat="server" />
</div>
</form>
</body>
</html>
Load Image to asp:Image
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!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 runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<asp:Label ID="Label1" runat="server" Height="32px" Text="Image to display" Width="176px"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" Width="112px">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
</asp:DropDownList>
<div>
<asp:Button ID="Button1" runat="server" Text="Select Image" /></div>
</div>
<div>
<asp:Image ID="Image1"
runat="server"
AlternateText="Image"
Height="128px"
ToolTip="Image"
Width="208px" /></div>
</div>
</form>
</body>
</html>
File: Default.aspx.vb
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Select Case DropDownList1.SelectedIndex
Case 0
Image1.ImageUrl = "http://www.nfex.ru/style/logo.png"
Case 1
Image1.ImageUrl = "http://www.nfex.ru/style/logo.png"
End Select
End Sub
End Class
Set ImageUrl and AlternateText for asp:Image (VB.net)
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub Page_Load()
Dim rnd As New Random()
Select Case rnd.Next(3)
Case 0
imgRandom.ImageUrl = "Picture1.gif"
imgRandom.AlternateText = "Picture 1"
Case 1
imgRandom.ImageUrl = "Picture2.gif"
imgRandom.AlternateText = "Picture 2"
Case 2
imgRandom.ImageUrl = "Picture3.gif"
imgRandom.AlternateText = "Picture 3"
End Select
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Show Image</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image
id="imgRandom"
Runat="server" />
</div>
</form>
</body>
</html>