.Net实现的批量删除(使用了repeater控件)

前台

 <asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate>
                <table>
                    <tr>
                        <td>选择</td>
                        <td>姓名</td>
                        <td>密码</td>
                    </tr>
                
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td><input id="CKChance" runat="server" type="checkbox" value='<%#Eval("AdminId") %>'/></td>
                    <td><%#Eval("AdminName") %></td>
                    <td><%#Eval("AdminPwd") %></td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
       
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

后台:

 protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                GetDate();
            }
        }

        public void GetDate() 
        {
            string ConStr = "Data Source=.;Initial Catalog=UserSystem;Integrated Security=True";
            using (SqlConnection conn = new SqlConnection(ConStr))
            {
                string sql = "SELECT * FROM ADMIN";
                //SqlCommand comm = new SqlCommand(sql.ToString(),conn);
                DataSet dt = new DataSet();
                SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
                sda.Fill(dt);
                DataTable dtl = dt.Tables[0];
                //dt.Tables.Add(dtl);
                this.Repeater1.DataSource = dtl;
                this.Repeater1.DataBind();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            DelInfo(); 
        }

        public void DelInfo() 
        {
            string j=string.Empty;

            for (int i = 0; i < this.Repeater1.Items.Count;i++ )
            {
                HtmlInputCheckBox cbox = (HtmlInputCheckBox)this.Repeater1.Items[i].FindControl("CKChance");
                if(cbox.Checked==true)
                {
                    j+=cbox.Value+",";                  
                }
            }
            j = (j + "").Replace(",!","");
            string ConStr = "Data Source=.;Initial Catalog=UserSystem;Integrated Security=True";
            using (SqlConnection conn = new SqlConnection(ConStr))
            {
                conn.Open();
                string delsql = "DELETE FROM [Admin] WHERE AdminId in (" + j + ")";
                SqlCommand comm = new SqlCommand(delsql.ToString(), conn);
                if (comm.ExecuteNonQuery() > 0)
                {
                    GetDate();
                   //Response.Write("<script>alert('删除成功')</script>");
                }
                else
                {
                    //return false;
                }
            }

        }

 

posted @ 2014-11-23 11:05  洛晨随风  阅读(273)  评论(0编辑  收藏  举报