DataList,PageDataSource打造简单的相册

我们一般可以使用 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);
        }

       
    }

 

 

转载于:https://www.cnblogs.com/chencaixia/archive/2007/12/10/989458.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值