我們一般可以使用 PageDataSource類來對(duì)Repeter,DataList等控件進(jìn)行分頁,。我們同樣也可以利用它來打造一個(gè)支持分頁的簡(jiǎn)單的相冊(cè)。
這個(gè)是頁面源碼,顯示圖片:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" /> <div align="center"> <asp:DataList ID="MainAlbum" runat="server" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" GridLines="Both" RepeatColumns="4" RepeatDirection="Horizontal"> <FooterStyle BackColor="#CCCCCC" /> <SelectedItemStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /> <ItemStyle BackColor="White" /> <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /> <ItemTemplate> <div> <a href='<%#"Photos/"+Eval("Name") %>' target="_blank" /> <asp:Image ID="Image1" runat="server" width="200" Height="160" ImageUrl='<%#"Photos/"+Eval("Name") %>' /> </div> </ItemTemplate> </asp:DataList></div> <div align="center"> <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"></asp:LinkButton> <asp:LinkButton ID="lbtnNext" runat="server" Text="下一頁" OnClick="lbtnNext_Click"></asp:LinkButton> </div> </form> 顯示圖片的后臺(tái)代碼: 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"); DataTable dtPhoto = new DataTable("Album");
DataColumn colSmall = new DataColumn("Name");
DataColumn colNormal = new DataColumn("Photo"); dtPhoto.Columns.Add(colSmall);
dtPhoto.Columns.Add(colNormal); //將圖片存入tabele中 for (int i = 0; i < (FileArray.Length); i++) ...{ 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 = 12; 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+"頁,當(dāng)前為"; MainAlbum.DataSource = Source; //MainAlbum.DataSource = ImageFile.GetFiles("*.jpg"); MainAlbum.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 btnMultiple_Click(object sender, EventArgs e)
...{ string FilePath = Server.MapPath("~/Photos/"); HttpFileCollection UploadFile = Request.Files; if (FileUpload1.HasFile || FileUpload2.HasFile || FileUpload3.HasFile || FileUpload4.HasFile || FileUpload5.HasFile) ...{ for (int i = 0; i < UploadFile.Count; i++) ...{ HttpPostedFile PostFile = UploadFile[i]; try ...{ if (PostFile.ContentLength > 0) ...{ string FileNames = PostFile.FileName; string SingleName = FileNames.Substring(FileNames.LastIndexOf("\") + 1); PostFile.SaveAs(FilePath + SingleName); } } catch (Exception ex) ...{ Assistant.AlertMessage(ex.Message, this); } } Response.Redirect("~/MainAlbum.aspx"); } else ...{ Assistant.AlertMessage("請(qǐng)輸入要上傳的文件", this); } } 本文來自CSDN博客,轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/oyjd614/archive/2007/11/18/1891282.aspx
|
|