在线相册,把源代码贴出来供大家学习

 

首先是App_Code文件夹下ImageFIleOperator.cs

 

ContractedBlock.gif ExpandedBlockStart.gif 图像文件操作类
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections.Generic;

public class ImageFileOperator//图像文件操作类
{
    
public List<string> GetAllImageFiles(string ImageFolder)//获得所有的图像文件清单
    {

        
if (Directory.Exists(ImageFolder))//判断是否存在此路径
        {
            
string[] files = Directory.GetFiles(ImageFolder);//返回指定目录中的文件
            List<string> imgs = new List<string>();//构造泛形集合对象imgs
            foreach (string file in files)
            {
                    imgs.Add(Path.GetFileName(file));
//根据路径获得文件名和其扩展名并添加道imgs里
            }
            
return imgs;
        }
        
return null;
    }
}

 

两个用户自定义用户控件

 

ContractedBlock.gif ExpandedBlockStart.gif ImageButtons
using System;
using System.Collections;
using System.Configuration;
using System.Data;
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.Collections.Generic;
using System.IO;

public partial class ImageButtons : System.Web.UI.UserControl
{
    
protected void Page_Load(object sender, EventArgs e)
    {
    }
    
/// 
    
/// 要显示为图片链接按钮的图片清单
    
/// 

    public List<string> Images
    {
        
get
        {
            
return ViewState["Images"== null ? null : ViewState["Images"as List<string>;
        }
        
set
        {
            ViewState[
"Images"= value;
            BuildImageButtons(value);
//构建图像按钮
        }
    }

    
/// 
    
/// 根据图像列表创建ImageButton对象
    
/// 

    
/// 
    private void BuildImageButtons(List<string> images)
    {
        
foreach (string img in images)
        {
            ImageButton btn 
= new ImageButton();//动态创建ImageButton
            btn.ImageUrl = ConfigurationManager.AppSettings["ImageFolder"+ img;//根据web.config中AppSettings节点设置的的ImageFolder读取虚拟路径
            btn.CommandName = "ImageButtonClick";//这句可有可无,因为后面我们并没有用到e.CommandName获取它,但是为了明确说明这些按钮是用来做什么的
            btn.CommandArgument = img;//这句可有可无,原因和上面一样,这个参数只是加载图片的名称和扩展名
            btn.Command += new CommandEventHandler(btn_Command);
            
//btn.Attributes.Add("onclick","javascript: return confirm('真的删除吗?');");//调用javascript验证是否删除
            btn.Attributes["onclick"= "return confirm('真的删除吗');";
            
//btn.OnClientClick = "return confirm('真的删除吗');";
            this.Controls.Add(btn);//将新创建的ImageButton追加到用户控件中
        }
    }

    
void btn_Command(object sender, CommandEventArgs e)
    {
        File.Delete(Server.MapPath((sender 
as ImageButton).ImageUrl));//根据点击的ImageButton删除响应的图片
        this.Controls.Remove(sender as ImageButton);//从用户控件中移出
    }


}

 

 

ContractedBlock.gif ExpandedBlockStart.gif ImageLinks
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
public partial class UserControl_ImageLinks : System.Web.UI.UserControl
{
    
public event CommandEventHandler ImageLnkClick;//定义图像链接事件
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    
public List<string> Images
    {
        
get
        {
            
return ViewState["Images"== null ? null : ViewState["Images"as List<string>;
        }
        
set
        {
            ViewState[
"Images"= value;
            BuildLinkButtons(value);
//构建链接按钮
        }
    }

    
private void BuildLinkButtons(List<string> images)
    {
        
foreach (string img in images)
        {
            LinkButton btn 
= new LinkButton();//动态创建LinkButton
            btn.Text = img + "    ";
            btn.CommandName 
= "ImageLnkClick";//这句话可有可无,但是它说明了这个按钮要触发一个单击事件
            btn.CommandArgument = img;//将图片的文件清单赋给按钮的命令参数,以变后面通过e.CommandArugment获取
            btn.Command += new CommandEventHandler(btn_Command);//挂接btn.Command事件响应函数btn_Command
            this.Controls.Add(btn);//将新创建的LinkButton追加到用户控件中
        }

    }

    
void btn_Command(object sender, CommandEventArgs e)
    {
        
if (ImageLnkClick != null)
            ImageLnkClick(sender, e);
//触发ImageLnkClick事件
    }
}

 

浏览图片页面.cs

 

ContractedBlock.gif ExpandedBlockStart.gif 浏览图片
using System;
using System.Collections;
using System.Configuration;
using System.Data;
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.Collections.Generic;

public partial class _Default : System.Web.UI.Page
{
    
private ImageFileOperator oper = new ImageFileOperator();//构造图像文件操作类

    
private List<string> imagesInFolder
    {
        
get
        {
            
return ViewState["imagesInFolder"== null ? null : ViewState["imagesInFolder"as List<string>;
        }
        
set
        {
            ViewState[
"imagesInFolder"= value;
        }
    }

    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (!IsPostBack)
        {
            
//第一次请求
            string imagefolder = Server.MapPath(ConfigurationManager.AppSettings["ImageFolder"]);//根据虚拟路径返回物理路径
            imagesInFolder = oper.GetAllImageFiles(imagefolder);//根据物理路径获得图片文件清单的泛型集合
        }
        ImageLinks1.Images 
= imagesInFolder;
        ImageLinks1.ImageLnkClick 
+= new CommandEventHandler(ImageLinks1_ImageLnkClick);
        
if (imagesInFolder.Count > 0)//判断是否有图片
            imgShow.ImageUrl = ConfigurationManager.AppSettings["ImageFolder"+ imagesInFolder[0];//默认绑定第一张图片
        else
            imgShow.Visible 
= false;//设置控件不可见


    }

    
void ImageLinks1_ImageLnkClick(object sender, CommandEventArgs e)//图片文件按钮事件被激发
    {
        imgShow.ImageUrl 
= ConfigurationManager.AppSettings["ImageFolder"+ e.CommandArgument;//绑定相关的图片
    }
}

 

 

上传图片页面.cs

 

ContractedBlock.gif ExpandedBlockStart.gif 上传图片
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class UpLoad : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        btnUpLoad.CssClass 
= "HeightStyle";
        FileUpload1.CssClass 
= "HeightStyle";
        Image1.Visible 
= false;
    }
    
protected void btnUpLoad_Click(object sender, EventArgs e)
    {
        
if (FileUpload1.HasFile)
        {
            FileUpload1.SaveAs(Server.MapPath(ConfigurationManager.AppSettings[
"ImageFolder"]) + FileUpload1.FileName);//上传图片文件
            Image1.ImageUrl = ConfigurationManager.AppSettings["ImageFolder"+ FileUpload1.FileName;//显示图片
            Image1.Visible = true;
        }
        
else
        {
            ClientScript.RegisterClientScriptBlock(
this.GetType(), "Info""alert('没有可以上传的图片');"true);
        }
    }
}

 

删除图片.cs

 

ContractedBlock.gif ExpandedBlockStart.gif 删除图片
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Delete : System.Web.UI.Page
{
    
private ImageFileOperator oper = new ImageFileOperator();
    
protected void Page_Load(object sender, EventArgs e)
    {
        
string imagefolder = Server.MapPath(ConfigurationManager.AppSettings["ImageFolder"]);
        ImageButtons1.Images 
= oper.GetAllImageFiles(imagefolder);
    }
}

 

css文件

ContractedBlock.gif ExpandedBlockStart.gif css
#container
{
    margin
-left:auto;
    margin
-right:auto;
    width:800px;
    padding:
0 0 0 0;
    border:solid 1px black;
}
#banner
{
    clear:both;
    text
-align:center;
    background
-image:url('banner.jpg');
    width:
100%;
    color:Black;
    vertical
-align:middle;
    line
-height:100px;
    font
-size:30px;
    height:100px;
}
#footer
{
    clear:both;
    text
-align:center;
    background
-image:url('footer.gif');
    background
-repeat:repeat-x;
    width:
100%;
    line
-height:30px;
    font
-size:15px;
    height:30px;
}
#right_panel
{
    top:0px;
    
float:left;
    right:0px;
    width:580px;
    margin:5px 0px 5px 5px;
}
#left_panel
{
    top:0px;
    
float:left;
    left:0px;
    width:180px;
    padding:10px;
    margin:5px 5px 5px 0px;
}
.block
{
    border:solid 1px  #6595D6;
    padding:1px;
    margin:4px 1px 4px 1px;
}
.block_header
{
    background
-color:#6595D6;
    height:
1.5em;
    padding
-top:0.3em;
    color:White;
    font
-size:12px;
    font
-weight:bold;
    vertical
-align:middle;
    margin
-top:2px;
}
.HeightStyle
{
    height:25px;
}

 

母板页

 

ContractedBlock.gif ExpandedBlockStart.gif 母板页
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

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>无标题页title>
    
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
head>
<body>
<div id="container">
    
<form id="form1" runat="server">
    
<div id="banner"><b>我的在线相册b>div>
    
<div id="left_panel">
        
<asp:TreeView ID="TreeView1" runat="server" ImageSet="BulletedList4" ShowExpandCollapse="False">
            
<ParentNodeStyle Font-Bold="False" />
            
<HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
            
<SelectedNodeStyle
                Font
-Underline="True" HorizontalPadding="0px" VerticalPadding="0px" ForeColor="#5555DD" />
            
<Nodes>
                
<asp:TreeNode Text="浏览图片" Value="浏览图片" NavigateUrl="~/Default.aspx">asp:TreeNode>
                
<asp:TreeNode Text="上传图片" Value="上传图片" NavigateUrl="~/UpLoad.aspx">asp:TreeNode>
                
<asp:TreeNode Text="删除图片" Value="删除图片" NavigateUrl="~/Delete.aspx">asp:TreeNode>
            
Nodes>
            
<NodeStyle Font-Names="Tahoma" Font-Size="10pt" ForeColor="Black" HorizontalPadding="5px"
                NodeSpacing
="0px" VerticalPadding="0px" />
        
asp:TreeView>
    
div>
    
    
<div id="right_panel">
        
<asp:contentplaceholder id="head" runat="server">
        
asp:contentplaceholder>
    
div>
    
    
<div id="footer"><b>2000-2008,All Rights Reservedb> div>
    
form>
  
div>
body>
html>

 

浏览图片.aspx

 

ContractedBlock.gif ExpandedBlockStart.gif 浏览图片.aspx
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="浏览图片" %>

<%@ Register Src="UserControl/ImageLinks.ascx" TagName="ImageLinks" TagPrefix="uc2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<div class="block">
<div class="block_header">
点击链接查看图片
div>
<div style="width:100%;">
    
<uc2:ImageLinks ID="ImageLinks1" runat="server" />
div>
    
<asp:Image ID="imgShow" runat="server" Width="98%" />
    
div>
    
asp:Content>

 

 

上传图片.aspx

 

ContractedBlock.gif ExpandedBlockStart.gif 上传图片.aspx
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="UpLoad.aspx.cs" Inherits="UpLoad" Title="上传图片" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<script language="javascript" type="text/javascript">
script>

<div class="block">
<div class="block_header">上传图片div>
<div>
    
<asp:FileUpload ID="FileUpload1" runat="server" Width="526px" />&nbsp;<asp:Button
        ID
="btnUpLoad" runat="server" OnClick="btnUpLoad_Click" Text="上传" />div>
div>
    
<asp:Image ID="Image1" runat="server" Width="98%" />
asp:Content>


 

 

删除图片.aspx

 

ContractedBlock.gif ExpandedBlockStart.gif 删除图片.aspx
<%@ Page Language="C#" Theme="主题1" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Delete.aspx.cs" Inherits="Delete" Title="删除图片" %>

<%@ Register Src="UserControl/ImageButtons.ascx" TagName="ImageButtons" TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<div class="block">
<div class="block_header">点击图片,删除相应图片div>
div> 
    
<uc1:ImageButtons id="ImageButtons1" runat="server">
    
uc1:ImageButtons>
asp:Content>

转载于:https://www.cnblogs.com/mdy41034264/archive/2008/12/11/1353089.html

你可能感兴趣的:(在线相册,把源代码贴出来供大家学习)