DataList,PageDataSource打造简单的相册

来源:互联网 发布:九阴绝学降级修为数据 编辑:程序博客网 时间:2024/06/11 09:55

我们一般可以使用 PageDataSource类来对Repeter,DataList等控件进行分页。我们同样也可以利用它来打造一个支持分页的简单的相册。

这个是页面源码,显示图片:

    <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>
                     
<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>

 

显示图片的后台代码:

    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+"页,当前为";
        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(
"请输入要上传的文件"this);
        }

       
    }