.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;
                }
            }

        }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。