1、前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataListPhotos_Page.aspx.cs" Inherits="DataSourceDemo.DataListPhotos_Page" %> <!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>Photos</title> <style> .ShowPage-font { font-size:12px; } </style> </head> <body> <form id="form1" runat="server"> <div> <asp:DataList ID="DataList1" runat="server" CellPadding="4" CellSpacing="2" GridLines="Both" RepeatColumns="3" RepeatDirection="Horizontal"> <ItemTemplate> <div> <a href='<%#"Photos/"+Eval("Name") %>' target="_blank" /><%--能够点击图片查看--%> <asp:Image ID="Image1" runat="server" width="160" Height="98" ImageUrl='<%#"Photos/"+Eval("Name") %>' /> </div> </ItemTemplate> </asp:DataList> <div class="ShowPage-font" > <asp:Label ID="lblPageCount" runat="server"></asp:Label> 第<asp:Label ID="lblCount" runat="server"></asp:Label>页 <asp:LinkButton ID="lbtnPreview" runat="server" Text="上一页" OnClick="lbtnPreview_Click" Font-Size="Small" ></asp:LinkButton> <asp:LinkButton ID="lbtnNext" runat="server" Text="下一页" OnClick="lbtnNext_Click" Font-Size="Small" ></asp:LinkButton> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="btnUploadFile" runat="server" Text="确定上传" Height="21px" onclick="btnUploadFile_Click" /> </div> </div> </form> </body> </html>
2、后台代码
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.IO; namespace DataSourceDemo { public partial class DataListPhotos_Page : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { lblCount.Text = "1"; BindPhotos(); } } private void BindPhotos() { string ImagePath = Server.MapPath("~/Photos/");//图片路径 DirectoryInfo ImageFile = new DirectoryInfo(ImagePath); FileInfo[] FileArray = ImageFile.GetFiles("*.jpg"); //得到目录下的所有jpg图片 DataTable dtPhoto = new DataTable("Album"); DataColumn colSmall = new DataColumn("Name"); DataColumn colNormal = new DataColumn("Photo"); dtPhoto.Columns.Add(colSmall); dtPhoto.Columns.Add(colNormal); for (int i = 0; i < (FileArray.Length); i++)//将图片存入table中 { DataRow Row = dtPhoto.NewRow(); Row["Name"] = FileArray[i].Name; Row["Photo"] = "./Photos/" + FileArray[i].Name; dtPhoto.Rows.Add(Row); } //分页 PagedDataSource Source = new PagedDataSource(); Source.AllowPaging = true; Source.DataSource = dtPhoto.DefaultView; Source.PageSize = 9; int CurrentPage = Convert.ToInt32(lblCount.Text); Source.CurrentPageIndex = CurrentPage - 1; lbtnPreview.Enabled = true; lbtnNext.Enabled = true; if (CurrentPage == 1) { lbtnPreview.Enabled = false; } if (CurrentPage == Source.PageCount) { lbtnNext.Enabled = false; } lblPageCount.Text = "共" + Source.PageCount + "页/"; DataList1.DataSource = Source; DataList1.DataBind(); } protected void lbtnNext_Click(object sender, EventArgs e)//下一页 { lblCount.Text = Convert.ToString(Convert.ToInt32(lblCount.Text) + 1); BindPhotos(); } protected void lbtnPreview_Click(object sender, EventArgs e)//上一页 { lblCount.Text = Convert.ToString(Convert.ToInt32(lblCount.Text) - 1); BindPhotos(); } protected void btnUploadFile_Click(object sender, EventArgs e)//图片上传 { string FilePath = Server.MapPath("~/Photos/"); HttpFileCollection UploadFile = Request.Files; if (FileUpload1.HasFile)//表示控件是否包含文件 { for (int i = 0; i < UploadFile.Count; i++) { HttpPostedFile postFile = UploadFile[i]; try { if (postFile.ContentLength > 0) { string FileName = postFile.FileName; string SingleName = FileName.Substring(FileName.LastIndexOf(@"") + 1); postFile.SaveAs(FilePath + SingleName); } } catch (Exception ex) { Assistant.AlertMessage(ex.Message, this);//Assistant是自定义的类,用于弹出窗口信息 } } Response.Redirect("~/DataListPhotos_Page.aspx"); } else { Assistant.AlertMessage("请输入要上传的文件", this); } } } }
3、Assistant类
public class Assistant { public static void AlertMessage(string msg, Page page) { page.ClientScript.RegisterStartupScript(page.GetType(), "", "<script language='javascript'>alert('" + msg + "');</script>"); } }
4、效果预览