在GridView中用户ID为1,2,3,4我点击全选后。我点击全选后怎么取得用户ID所有的值。我只会在GridView中取单个的值。取全部的值是怎么弄?

解决方案 »

  1.   

    foreach (GridViewRow row in GridView)
    {
        //取出每一行中的ID
    }
      

  2.   

    你要用for循环找到你的控件
        //全选和反选  
    如果你要取值雷同
        protected void ckbAll_CheckedChanged(object sender, EventArgs e) //注ckbAll为你全选的那个复选框控件
        {
            
            CheckBox ckbAll = (CheckBox)sender;
            
            if (ckbAll.Checked == true)
            {
                for (int i = 0; i < this.gvManage.Rows.Count; i++)  //注gnManage为你的GridView
                {
                    CheckBox ckbSend = (CheckBox)this.gvManage.Rows[i].Cells[0].FindControl("ckbOne");
                    if (ckbSend != null)
                    {
                        ckbSend.Checked = true;
                    }
                }
            }
            else
            {
                for (int i = 0; i < this.gvManage.Rows.Count; i++)
                {
                    CheckBox ckbSend = (CheckBox)this.gvManage.Rows[i].Cells[0].FindControl("ckbOne");
                    if (ckbSend != null)
                    {
                        ckbSend.Checked = false;
                    }
                }
            }    }
      

  3.   

    还有要注意ckbOne为你要找的那个控件的名称
      

  4.   

    参考一下下面的代码,是一个用来获取所有选中ID的,方法返回了一个ID组成的字符串,可以在使用的时候拆分开来: private string getSelectedIDS()
    {
    string strDeleteIDS = null;
    GridView grid = this.grdBooks;
    for (int i = 0; i < grid.Rows.Count; i++)
    {
    CheckBox checkBox = grid.Rows[i].FindControl("checkBox") as CheckBox;
    if (checkBox != null && checkBox.Checked)
    {
    strDeleteIDS+= grid.DataKeys[i].Value + "|";
    }
    }
    return strDeleteGuid;
    }
      

  5.   

    补充LS的:
    GridView用到了DataKeys,需要设置DataKeyNames属性。