foreach(GridViewRow gr in GridView1.Rows)
            {
                CheckBox chb = (CheckBox)gr.FindControl("chb_check");
                if (chb.Checked)
                {
                    string stuID = gr.Cells[2].Text.ToString();
                    string stuName = gr.Cells[3].Text.ToString();
                }
            }
我调试的时候stuID,stuName都为空,请问cells[n].Text取的是项中那个数据呢,ItemTemplate,EditItemTemplate??我已经把该列转为模板列了.ItemTemplate,EditItemTemplate里都有控件?我就是想取控件中的值,不想再用findcontrol这个方法

解决方案 »

  1.   

    楼主想取出模板中控件的值,不想用findcontrol 那想用什么?我只会这个...
      

  2.   

     gr.Cells[2].Text.ToString(); 转为模板列后这样不能取出单元格里面的值吗?
      

  3.   

    如果已经是模板列,用Cells[index].Text是得不到里面的内容的,必须使用FindControl方法,其实理由很简单,TemplateField里面可以有多个控件,比如文本框,标签等等,你取里面的Cells[index].Text,取的算是什么呢?CheckBox chb;
    string stuID = string.Empty;
    string stuName = string.Empty;
    foreach (GridViewRow gr in GridView1.Rows)
    {
        chb = gr.FindControl("chb_check") as CheckBox;
        if (chb != null && chb.Checked)
        {
            stuID = (gr.FindControl("id文本框的id") as TextBox).Text;
            stuName = (gr.FindControl("name文本框的id") as TextBox).Text;
        }
      

  4.   


    同意3楼,微软搞FindControl就是做这个用的!!