我們一般可以使用 PageDataSource類來對Repeter,DataList等控件進行分頁。我們同樣也可以利用它來打造一個支持分頁的簡單的相冊。
這個是頁面源碼,顯示圖片:
<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>
顯示圖片的後臺代碼:
{
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+"頁,當前爲";
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();
}
批量上傳的代碼:
{
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("請輸入要上傳的文件", this);
}
}