protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            int i = 0;
            foreach (TableCell tc in e.Row.Cells)
            {
                if (tc.Text == "1")
                {
                    CheckBox cb = new CheckBox();
                    cb.LabelAttributes.Add("modelname", e.Row.Cells[1].Text);
                    cb.LabelAttributes.Add("columnid", i.ToString());
                    tc.Controls.Add(cb);
                }
                else if (tc.Text == "0" || tc.Text == null) tc.Text = "";
                i++;
            }
        }
这是我动态添加的checkbox,现在想在databound方法中对其值进行设定,请问怎么做啊

解决方案 »

  1.   

    动态添加的时候不是可以指定CheckBox的Name吗,后面用的时候用findControl()然后强转成CheckBox,
      

  2.   

    参考一下protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                foreach (Control c in e.Row.Controls)
                {
                    if (c.GetType() == typeof(CheckBox))
                    {
                        CheckBox cb = (CheckBox)c;
                    }
                }
            }
        }要注意是HeaderRow 、 DataRow 还是FooterRow 。
      

  3.   


    foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("CheckBoxID");
            }
      

  4.   

    构造ChecBox ID时要规律,例如,统一前缀为cb_,后缀跟GridView行索引相同,这样用for来遍历时方便构造CheckBox ID
      

  5.   


    要在databound方法中实现,谢谢……另外,foreach找子控件的方法用过,写了3层都没找到checkbox……
      

  6.   

        //为什么不在页面上的行内加CheckBox?
        //这样你在DataBound时只需要据条件给CheckBox赋值,隐藏等操作就好了//取值还好取
      

  7.   

    添加checkbox的name 名字为  cbk+rowid    这样你在下面循环的用fildcontrol()获取每一个控件的值不就行了吗
      

  8.   


    你是在RowDataBound中添加的CheckBox?
    你要在哪里找动态创建的CheckBox
      

  9.   

    我暂时是这个思路,用rowid+i,作为ID
    但是总感觉有漏洞
    而且实际我这样写也出现了问题protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                int i = 2;
                foreach (TableCell tc in e.Row.Cells)
                {
                    if (tc.Text == "1")
                    {
                        CheckBox cb = new CheckBox();
                        cb.ID = e.Row.RowIndex.ToString() + i.ToString();
                        cb.LabelAttributes.Add("modelname", e.Row.Cells[1].Text);
                        cb.LabelAttributes.Add("columnid", i.ToString());
                        tc.Controls.Add(cb);
                    }
                    else if (tc.Text == "0" || tc.Text == null) tc.Text = "";
                    i++;
                }
            }        protected void GridView1_DataBound(object sender, EventArgs e)
            {            for (int i = 0; i < ((DataTable)GridView1.DataSource).Rows.Count; i++)
                {
                    for (int j = 0; j < ((DataTable)GridView1.DataSource).Columns.Count; j++)
                    {
                        CheckBox ctrl = new CheckBox();
                        ctrl = (CheckBox)GridView1.Rows[i].FindControl(i.ToString() + j.ToString());
                        if (ctrl is CheckBox)
                        {
                            string[] param = new string[3];
                            param[0] = ddlPersonName.SelectedValue.ToString();
                            param[1] = ((CheckBox)ctrl).Attributes["modelname"];
                            param[2] = GridView1.HeaderRow.Cells[Convert.ToInt32(((CheckBox)ctrl).Attributes["colunmid"])].Text;
                            DataTable funstate = controlDataBll.loadValue("loadfunstate", param);                        if (funstate.Rows[0][0].ToString() == "1")
                            {
                                ((CheckBox)ctrl).Checked = true;
                            }
                            else if (funstate.Rows[0][0].ToString() == "0")
                            {
                                ((CheckBox)ctrl).Checked = false;
                            }
                        }
                    }
                }
            }
    现在这样写在param[1] = ((CheckBox)ctrl).Attributes["modelname"];
                            param[2] = GridView1.HeaderRow.Cells[Convert.ToInt32(((CheckBox)ctrl).Attributes["colunmid"])].Text;
    处获得attributes时得到的都是空值
    P.S.:gridview1有6列,第一列序号,第二列modelname,然后4列checkbox(可能有可能没有)
      

  10.   

    饿,打完就发现原来自己错把labelattributes写成attributes了
      

  11.   

    在创建控件时不能指定它的属性吗?
    CheckBox cb = new CheckBox();
    cb.Checked = ...
      

  12.   

    可以,但是写在rowdatabound里我不知道怎么获得列名……尴尬啊……
      

  13.   

     foreach (DataRow dr_Col in dtGrid.Rows)
                        {                       
                            t_NOTICE_USER.USERID = dr_Col.ItemArray[0].ToString();
    }
      

  14.   

    [align=left]foreach (GridViewRow row in this.gvshow.Rows)
    {
        Control ctrl = row.FindControl("cblist");
        if ((ctrl as CheckBox).Checked)
        {
           //写代码
        }
    }