繁体   English   中英

仅选择复选框删除多个asp.net转发器项目

[英]Delete multiple asp.net repeater Items with checkbox selection only

这里只显示页面加载时不显示任何错误是什么错误?

C#代码

protected void imgs_Click(object sender, ImageClickEventArgs e)  
        {        
            foreach (RepeaterItem ri in repeater.Items)
            {

            CheckBox item_check = (CheckBox)ri.FindControl("item_check");
            Label txt_id = (Label)ri.FindControl("txt_id");

                if (item_check.Checked)
                {
                    con = new SqlConnection(strcon);
                    SqlCommand cmd = new SqlCommand("ram", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    con.Open();
                    cmd.Parameters.AddWithValue("@Action", "DELETE");
                    cmd.Parameters.AddWithValue("@eid", txt_id);
                    repeat();
               }                
        }     
}

asp.code

您忘记编写cmd.ExecuteNonQuery();

  1. 停止使用AddWithValue
  2. 始终用于实现IDisposable SqlConnectionSqlCommand using

    受保护的void imgs_Click(对象发送者,ImageClickEventArgs e)
    {

      foreach (RepeaterItem ri in repeater.Items) { CheckBox item_check = (CheckBox)ri.FindControl("item_check"); Label txt_id = (Label)ri.FindControl("txt_id"); if (item_check.Checked) { Using(SqlConnection con = new SqlConnection(strcon)) { Using(SqlCommand cmd = new SqlCommand("ram", con)) { cmd.CommandType = CommandType.StoredProcedure; con.Open(); cmd.Parameters.Add("@Action",SqlDbType.Varchar,50).Value="DELETE"; cmd.Parameters.Add("@eid",SqlDbType.Int).Value=Convert.ToInt16(txt_id.Text); cmd.ExecuteNonQuery(); repeat(); } } } } 

    }

    foreach (RepeaterItem ri in repeater.Items)
    {

        CheckBox item_check = (CheckBox)ri.FindControl("item_check");
        Label txt_id = (Label)ri.FindControl("txt_id");

        if (item_check.Checked)
        {

            con = new SqlConnection(strcon);
            cmd = new SqlCommand("ram", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Action", "DELETE");
            cmd.Parameters.AddWithValue("@eid", txt_id.Text);
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
            finally
            {

                con.Close();
            }
        }
    }
    repeat();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM