复选框删除错误 [英] checkbox delete error

查看:85
本文介绍了复选框删除错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我尝试了复选框删除,但它不起作用,而且它也没有出现错误,它只是loading.plz任何人都可以帮助我....(我使用lblselectdel获取ID并从该即时消息中删除)


i tried checkbox delete but it is not working and also its not shoeing error its just loading.plz any one help me....(im using lblselectdel to get the id and from that im delting)

foreach (GridViewRow row in GridView1.Rows)
       {
           CheckBox cs = (CheckBox)row.Cells[0].FindControl("chkchild");
           if (cs.Checked == true)
           {
               lblselectdel.Text = row.Cells[1].Text.ToString();

               con.Open();
               SqlCommand cmd2 = new SqlCommand("Delete from  Registration where MemID='" + lblselectdel.Text + "'", con);
               cmd2.ExecuteNonQuery();
               SqlDataAdapter ada = new SqlDataAdapter(cmd2);
               cmd2.CommandType = CommandType.Text;
               cmd2.CommandText = "select *from Registration";
               DataTable dt = new DataTable();
               ada.Fill(dt);
               GridView1.DataSource = dt;
               GridView1.DataBind();

               con.Close();
           }
       }

推荐答案

您确定以下内容确实返回了您想要的正确ID?
You sure that the following does return correct ID you want?
lblselectdel.Text = row.Cells[1].Text.ToString();



将实现更改为:
1.定义DatatKeyName



Change the implementation to:
1. Define DatatKeyName

<asp:GridView ID="Products" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID">



2.现在访问ID值如下:



2. Access ID value now like:

CheckBox cb = (CheckBox)row.FindControl("chkchild");
if (cb != null && cb.Checked)
{
    // Get the ProductID for the selected row
    int productID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
    
   // Now your delete logic
}



尝试!



Try!


我可以看到的一个问题是,在删除遍历行的同一循环内的每个元素之后,您将再次绑定.

拆下滚边.然后尝试一下. (我假设其他东西都可以用)

One problem I can see is that your are binding again after deleting each element inside the same loop that walks through the rows.

remove the bingding out side.. and give it a try. (I am assuming the other stuff works)

con.Open();
foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox cs = (CheckBox)row.Cells[0].FindControl("chkchild");
            if (cs.Checked == true)
            {
                lblselectdel.Text = row.Cells[1].Text.ToString();               
                SqlCommand cmd2 = new SqlCommand("Delete from  Registration where MemID='" + lblselectdel.Text + "'", con);
                cmd2.ExecuteNonQuery();  
            }
        }
// you could change this with a DataBind() only. if you didnt change the datasource
  SqlCommand cmd = new SqlCommand();
  cmd.CommandType = CommandType.Text;
  SqlDataAdapter ada = new SqlDataAdapter(cmd);
  cmd.CommandText = "select *from Registration";
  DataTable dt = new DataTable();
  ada.Fill(dt);
  GridView1.DataSource = dt;
  GridView1.DataBind();
  con.Close();




打开数据库连接时也要考虑用户try-catch-finally :)希望能有所帮助.




Also consider user try-catch-finally when opening db connections :) hope it helps.


这篇关于复选框删除错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆