代码如下:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void btnAdd_Click(object sender, EventArgs e)
{
if (upImage.HasFile)
{
if (CheckFileType(upImage.FileName))
{
String filePath = "~/UploadImages/" + upImage.FileName;
upImage.SaveAs(MapPath(filePath));
}
}
}
bool CheckFileType(string fileName)
{
string ext = Path.GetExtension(fileName);
switch (ext.ToLower())
{
case ".gif":
return true;
case ".png":
return true;
case ".jpg":
return true;
case ".jpeg":
return true;
default:
return false;
}
}
void Page_PreRender()
{
string upFolder = MapPath("~/UploadImages/");
DirectoryInfo dir = new DirectoryInfo(upFolder);
dlstImages.DataSource = dir.GetFiles();
dlstImages.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>FileUpload File</title>
</head>
<body>
<form runat="server">
<div>
<asp:Label
Text="Image File:"
AssociatedControl
Runat="server" />
<asp:FileUpload
Runat="server" />
<br /><br />
<asp:Button
Text="Add Image"
OnClick="btnAdd_Click"
Runat="server" />
<hr />
<asp:DataList
RepeatColumns="3"
runat="server">
<ItemTemplate>
<asp:Image
ImageUrl='<%# Eval("Name", "~/UploadImages/{0}") %>'
Runat="server" />
<br />
<%# Eval("Name") %>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
请问1.ImageUrl='<%# Eval("Name", "~/UploadImages/{0}") %>'这句话是什么意思?
2.<%# Eval("Name") %> 这样怎么就会把文件名输出来了呢?
你看看模板绑定是怎么绑定就知道了
回去看书 回去看书!
...
你看下后台DataList是用什么绑定的,那Name就是绑定表中的Name字段。如果不懂建议看看MSDN中<%=%>和<%#%>