难点:动态添加CheckBox的代码若没有放到Page_Load的if(!ispostback)里,则每次回发后,服务器都将重新加载checkbox,所以checkbox的CheckChanged事件将无法触发。
解决办法:在 Page_Init事件里利用ViewState保存一组信息(被选中的所有CheckBox的ID),同时,将所有动态添加的checkbox的AutoPostBack设为false。
实现效果如下:

Html代码:


<td align="left" class="style25">
文物名称:<asp:Label ID="Label10" runat="server" Font-Bold="True" Font-Size="Medium"
ForeColor="Red" Text="已经没有了!!"></asp:Label>
</td>
<td>
</td>
</tr>
<tr>
<td class="style23">
</td>
<td class="style24" align="center">
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</td>
<td>
</td>
</tr>
<tr>
<td class="style23">
</td>
<td class="style24">
<asp:Button ID="ActiveButton" runat="server" oncommand="DeleteImage"
Text="删除选中图片" Visible="False" onclick="ActiveButton_Click" />
</td>
<td>
</td>
</tr>
<tr>
<td class="style23">
</td>
<td class="style24">
后台部分代码如下:
1.Page_Init
1
protected
void
Page_Init(
object
sender, EventArgs e)
2
{
3
for (int i = 0; i < 6; i++)
4
{
5
ViewState["cb" + i.ToString()] = Request.Form["cb" + i.ToString()];
6
}
7
}

2



3

4



5

6

7

2.DataBindToImage()函数
1
protected
void
DataBindToImage()
2
{
3
this.Panel1.Controls.Add(new LiteralControl("<font size=3><center>文物已保存有如下图片:</center></font><br>"));
4
文物 obj = new 文物();
5
object[] Pics = obj.GetPics(Convert.ToInt32(Request.QueryString["文物_ID"]));
6
TwoMuseum.BLL.Heritage.文物图片 it3 = new TwoMuseum.BLL.Heritage.文物图片();
7
8
int i = Pics.Length-1;
9
10
while (i>-1 )
11
{
12
Image im = new Image();
13
im.ID = i.ToString();//
14
15
//it3.writeFile((byte[])Pics[i], "UI\\pic\\Heritage" + i.ToString() + ".jpg");
16
//im.ImageUrl = "~/pic/Heritage" + i.ToString() + ".jpg";
17
18
it3.writeFile((byte[])Pics[i], Server.MapPath(".") + "/pic/Heritage" + i.ToString() + ".jpg");
19
im.ImageUrl = "~/pic/Heritage" + i.ToString() + ".jpg";
20
21
CheckBox cb = new CheckBox();
22
cb.Text = "选中删除";
23
cb.Font.Size = System.Web.UI.WebControls.FontUnit.Small;
24
cb.ID = "cb"+i.ToString();//
25
cb.Checked = false;
26
cb.AutoPostBack = false;
27
cb.CheckedChanged+=new EventHandler(cb_CheckedChanged);
28
cb.Attributes.Add("value",i.ToString());
29
//cb.Attributes.Add("onclick", "javascript:check(this,"+Check+")");
30
31
this.Panel1.Controls.Add(im);
32
this.Panel1.Controls.Add(cb);
33
if (0 == i % 2)
34
this.Panel1.Controls.Add(new LiteralControl("<br><br>"));
35
36
//this.Panel1.Controls.Add(new LiteralControl(";nbsp ;nbsp ;nbsp ;nbsp ;nbsp ;nbsp ;nbsp "));
37
38
i--;
39
}
40
41
this.ActiveButton.Visible = true;
42
this.ActiveButton.Attributes.Add("onclick", "javascript:return confirm('你确认要删除所有选中图片吗?')");
43
44
if (Pics.Length > 0)
45
{
46
this.Panel1.Controls.Add(new LiteralControl("<br>"));
47
this.Panel1.Controls.Add(new LiteralControl("<br>"));
48
49
this.ActiveButton.CommandArgument = Pics.Length.ToString();//--------------为按钮添加属性
50
}
51
52
this.Label10.Text = ViewState["文物名称"].ToString();
53
for (int i2 = 0; i2 < 6; i2++)
54
{
55
Flag[i2] = false;
56
}
57
}

2



3

4

5

6

7

8

9

10

11



12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45



46

47

48

49

50

51

52

53

54



55

56

57

3.所有checkbox共同的事件cb_CheckedChanged:
1
protected
void
cb_CheckedChanged(
object
sender, EventArgs e)
2
{
3
Response.Write("点击了CheckBox!!");
4
5
CheckBox cbx=(CheckBox)sender;
6
if (cbx.Checked)
7
{
8
string Cid=cbx.ID.ToString().Substring(2,1);
9
ViewState["cb" + Cid] = 1;
10
11
}
12
}

2



3

4

5

6

7



8

9

10

11

12

4.另外,因为读取的图片都是二进制形式,所以每次都是将图片先转换到临时文件夹,然后在处理
因而,必须在读取图片前先对临时图片文件夹进行清空操作,以免读取了错误的图片,清空的函数如下:
1
/**/
///------------------------------删除文件夹内图片-----------------------------
2
public
void
DeleteImg(
string
path)
3
{
4
string FilePath = path;
5
for (int i = 0; i < 6; i++)
6
{
7
FilePath = path + i.ToString() + ".jpg";
8
if (File.Exists(FilePath)) //------------- 判断,如果存在就删除
9
File.Delete(FilePath);
10
11
FilePath =path + "Heritage" + i.ToString() + ".jpg";
12
if (File.Exists(FilePath)) //------------- 判断,如果存在就删除
13
File.Delete(FilePath);
14
}
15
}


2

3



4

5

6



7

8

9

10

11

12

13

14

15

5.删除图片的按钮ActiveButton的事件


protected void DeleteImage(object sender, EventArgs e) //---------------------删除勾选的图片------------------------------
{
int imgNum = Convert.ToInt32((((Button)sender)).CommandArgument);
//==---------------------------------------------------------------为了获取文物图片的编号
TwoMuseum.Entity.Heritage.文物图片查询Info 文物图片查询=new 文物图片查询Info();
文物图片查询.SetValue("文物编号",Request.QueryString["文物_ID"]);
文物图片查询.SetVisble("文物图片编号");
TwoMuseum.BLL.Heritage.文物图片 文物图片 = new 文物图片();
DataTable dt = 文物图片.Search(文物图片查询);
DataRow dr;
for (int i = imgNum-1; i >-1 ; i--)
{
if (ViewState["cb" + i.ToString()] != null)
//if (Check[i]!=null)
{
dr = dt.Rows[i];
string 文物图片_ID = dr["文物图片编号"].ToString();
if (文物图片.Delete(文物图片_ID).Vlus)
this.Panel1.Controls.Add(new LiteralControl("<font color=red>删除图片成功!</font><br>"));
else
this.Panel1.Controls.Add(new LiteralControl("<font color=red>删除图片失败,数据库操作发生错误!!</font>"));
}
}
DataBindToImage();
}
另外,图片绑定的函数在page_load里调用,但不包含在if(!IspostBack)内。。