aspx
cs
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;
public partial class _Default : System.Web.UI.Page
{
private System.Collections.Generic.List<TableCell> cellList
= new System.Collections.Generic.List<TableCell>();
private int num;
private int numtwo;
private string[] name;
protected void Page_Load(object sender, EventArgs e)
{
ShowView();
}
private void ShowView()
{
string vpath = "~/summer";
string path = this.MapPath(vpath);
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(path);
System.IO.FileInfo[] files = di.GetFiles("*.jpg");
// 首先创建整个的表格
// 创建一个保存所有格的集合
this.Table1.Rows.Clear();
cellList.Clear();
int lineCount = (files.Length + 2) / 3;
num = lineCount;
numtwo = files.Length;
name = new string[files.Length];
for (int i = 0; i < lineCount; i++)
{
TableRow row = new TableRow();
this.Table1.Rows.Add(row);
for (int j = 0; j < 3; j++)
{
TableCell cell = new TableCell();
row.Cells.Add(cell);
cellList.Add(cell);
}
}
// 将图片加入到格中
for (int i = 0; i < files.Length; i++)
{
System.IO.FileInfo fi = files[i];
Image img = new Image();
img.Width = Unit.Pixel(200);
img.Height = Unit.Pixel(160);
img.ImageUrl = string.Format("~/summer/{0}", fi.Name);
name[i] = fi.Name;
cellList[i].Controls.Add(img);
CheckBox chb = new CheckBox();
chb.ID = "checkbox" + i.ToString();
Label lbl = new Label();
lbl.Text = fi.Name;
Literal br1 = new Literal();
br1.Text = "<br/>";
cellList[i].Controls.Add(br1);
cellList[i].Controls.Add(lbl);
cellList[i].Controls.Add(chb);
Literal br2 = new Literal();
br2.Text = "<br/>";
cellList[i].Controls.Add(br2);
Button btn = new Button();
btn.ID = string.Format("BtnDelete{0}", i);
btn.Text = fi.Name;
// 注册事件处理方法
btn.Click += new EventHandler(BtnDelete_Click);
cellList[i].Controls.Add(btn);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string vpath = "~/summer";
string path = this.MapPath(vpath);
for (int i = 0; i < numtwo; i++)
{
CheckBox cbx = (CheckBox)cellList[i].Controls[3];
if (cbx.Checked)
{
string filepath = System.IO.Path.Combine(path, name[i]);
System.IO.File.Delete(filepath);
}
}
ShowView();
}
protected void BtnDelete_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
string filename = btn.Text;
string vpath = "~/summer";
string path = this.MapPath(vpath);
string filepath = System.IO.Path.Combine(path, filename);
System.IO.File.Delete(filepath);
ShowView();
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
for (int i = 0; i < numtwo; i++)
{
CheckBox cbx = (CheckBox)cellList[i].Controls[3];
cbx.Checked = true;
}
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
for (int i = 0; i < numtwo; i++)
{
CheckBox cbx =(CheckBox) cellList[i].Controls[3];
cbx.Checked = false;
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: center">
<br />
<br />
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">全选</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">清空</asp:LinkButton><br />
<br />
<asp:Table ID="Table1" runat="server">
</asp:Table>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Button" /></div>
</form>
</body>
</html>