动态读取二进制图片并绑定CheckBox实现批量删除图片[原创]

实现功能:动态读取二进制图片并绑定CheckBox,然后在页面中选中一个或多个CheckBox实现批量删除。
难点:动态添加CheckBox的代码若没有放到Page_Load的if(!ispostback)里,则每次回发后,服务器都将重新加载checkbox,所以checkbox的CheckChanged事件将无法触发。
解决办法:在 Page_Init事件里利用ViewState保存一组信息(被选中的所有CheckBox的ID),同时,将所有动态添加的checkbox的AutoPostBack设为false。

实现效果如下:


Html代码:
ContractedBlock.gif ExpandedBlockStart.gif Code
       <td align="left" class="style25">
                                        文物名称:
<asp:Label ID="Label10" runat="server" Font-Bold="True" Font-Size="Medium" 
                                            ForeColor
="Red" Text="已经没有了!!"></asp:Label>
                                    
</td>
                                    
<td>
                                        
&nbsp;</td>
                                
</tr>
                                
<tr>
                                    
<td class="style23">
                                        
&nbsp;</td>
                                    
<td class="style24" align="center">
                                        
<asp:Panel ID="Panel1" runat="server">
                                        
</asp:Panel>
                                    
</td>
                                    
<td>
                                        
&nbsp;</td>
                                
</tr>
                                
<tr>
                                    
<td class="style23">
                                        
&nbsp;</td>
                                    
<td class="style24">
                                        
                                        
<asp:Button ID="ActiveButton" runat="server" oncommand="DeleteImage" 
                                            Text
="删除选中图片" Visible="False" onclick="ActiveButton_Click" />
                                    
</td>
                                    
<td>
                                        
&nbsp;</td>
                                
</tr>
                                
<tr>
                                    
<td class="style23">
                                        
&nbsp;</td>
     
         
  
                    <td class="style24">


后台部分代码如下:
1.Page_Init
                  
1   protected   void  Page_Init( object  sender, EventArgs e)
2 ExpandedBlockStart.gifContractedBlock.gif         {
3            for (int i = 0; i < 6; i++)
4ExpandedSubBlockStart.gifContractedSubBlock.gif            {
5                ViewState["cb" + i.ToString()] = Request.Form["cb" + i.ToString()];
6            }
            
7        }

2.DataBindToImage()函数
 1   protected   void  DataBindToImage()
 2 ExpandedBlockStart.gifContractedBlock.gif         {
 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 )
11ExpandedSubBlockStart.gifContractedSubBlock.gif            {
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)
45ExpandedSubBlockStart.gifContractedSubBlock.gif            {
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++)
54ExpandedSubBlockStart.gifContractedSubBlock.gif            {
55                Flag[i2] = false;
56            }

57        }

3.所有checkbox共同的事件cb_CheckedChanged:

 1   protected   void  cb_CheckedChanged( object  sender, EventArgs e)
 2 ExpandedBlockStart.gifContractedBlock.gif         {
 3            Response.Write("点击了CheckBox!!");
 4
 5            CheckBox cbx=(CheckBox)sender;
 6            if (cbx.Checked)
 7ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 8                string Cid=cbx.ID.ToString().Substring(2,1);
 9                ViewState["cb" + Cid] = 1;
10
11            }

12        }


4.另外,因为读取的图片都是二进制形式,所以每次都是将图片先转换到临时文件夹,然后在处理
因而,必须在读取图片前先对临时图片文件夹进行清空操作,以免读取了错误的图片,清空的函数如下:
 1 ExpandedBlockStart.gif ContractedBlock.gif   /**/ ///------------------------------删除文件夹内图片-----------------------------
 2          public    void  DeleteImg( string  path)
 3 ExpandedBlockStart.gifContractedBlock.gif         {
 4            string FilePath = path;
 5            for (int i = 0; i < 6; i++)
 6ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 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        }


5.删除图片的按钮ActiveButton的事件
ContractedBlock.gif ExpandedBlockStart.gif Code
 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)内。。

转载于:https://www.cnblogs.com/LinderMan/archive/2009/07/25/1530920.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值