调用代码如下:
default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 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></title>
<script type="text/javascript" language="javascript">
function showChose() {
//以弹出窗口的形式 弹出头像选择窗口 并接受返回值
var src = window.showModalDialog("Face.aspx", "", "width:480px;height:480px;menu:no;tool:no;status:no;");
//将返回的图片 显示出来
document.getElementById("imgFace").src = src;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<!--用户单击此层 弹出头像选择窗口 -->
<span style="color:Red;cursor:hand;" οnclick="showChose()">选择头像</span>
<img id="imgFace" />
</div>
</form>
</body>
</html>
被调用页面代码如下:
face.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Face.aspx.cs" Inherits="Face" %>
<%@ Import Namespace="System.IO" %>
<!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></title>
<script type="text/javascript" language="javascript">
//当用户单击头像图片时 执行的JavaScript脚本
function returnFace(src) {
//关闭当前窗体 并返回用户选择的头像的URL
window.opener = "";
window.returnValue = src;
window.close();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<%
//获取头像文件夹
DirectoryInfo dic = new DirectoryInfo(this.Server.MapPath("Faces"));
int i = 0;
//循环遍历头像文件
foreach (FileInfo f in dic.GetFiles())
{
if (f.Extension == ".bmp")
{
%>
<!---生成头像显示图片框--->
<img src='< <%
i++;
//每显示10章图片 即换行
if (i % 10 == 0)
{
Response.Write("<br/>");
}
continue;
}
}
%>
</div>
</form>
</body>
</html>
当不考虑效率时,或工期比较紧时,可考虑用这种方法。