日期:2014-05-18  浏览次数:21164 次

gridview里checkbox选择的问题
我的gridvew里放了一列checkbox按钮,在删除时我先判断选中的行数,然后弹出一个确认窗口,但是第一次点击时没有出,第二次时才弹出窗口。之后每次一次弹出窗口选择的数即下文中的目count都是上一次选择的,不知道各位有没有遇到过这个问题。怎么解决?


protected void btDelete_Click(object sender, EventArgs e)
  {
  int count = 0;
  for (int i = 0; i < GridView1.Rows.Count; i++)
  {
  CheckBox Cb = (CheckBox)GridView1.Rows[i].FindControl("Checkbox");
  if (Cb.Checked)
  {
  count++;
  }
  }
  if (0 == count)
  {
  Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('请选择删除项')</script>");
  }
  else
  {
  this.btDelete.Attributes.Add("onclick","return confirm('确定删除这" +count.ToString()+ "项么?');");
  for (int j = 0; j < GridView1.Rows.Count; j++)
  {
  CheckBox Cb = (CheckBox)GridView1.Rows[j].FindControl("Checkbox");
  if (Cb.Checked)
  {
  int id = Convert.ToInt32(GridView1.DataKeys[j]["id"].ToString());


  int flag = Productbll.proDel(id);
   
  }
  }
  if (this.ddlProductLine.SelectedValue == "please choose")
  {
  MorenBind();
  }
  else
  {
  Bind();
  }
   
  }
  }

------解决方案--------------------
CS代码
C# code

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Button1.Attributes.Add("onclick", "javascript:Confirm();");
        }
    }


    protected void btDelete_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(HiddenField1.Value) && int.Parse(HiddenField1.Value) > 0)
        {
            for (int j = 0; j < GridView1.Rows.Count; j++)
            {
                CheckBox Cb = (CheckBox)GridView1.Rows[j].FindControl("Checkbox");
                if (Cb.Checked)
                {
                    int id = Convert.ToInt32(GridView1.DataKeys[j]["id"].ToString());


                    int flag = Productbll.proDel(id);

                }
            }

            if (this.ddlProductLine.SelectedValue == "please choose")
            {
                MorenBind();
            }
            else
            {
                Bind();
            }
        }
    }