这样的值如何获得???
  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
 if (e.Row.RowType == DataControlRowType.DataRow)
        {
e.Row.Cells[e.Row.Cells.Count - 1].Text = "<input type='checkbox' runat='server' id='checkbox' value='"+GridView1.DataKeys[e.Row.RowIndex].Value+"'>";HtmlInputCheckBox checkbox = (HtmlInputCheckBox)e.Row.Cells[e.Row.Cells.Count - 1].FindControl("checkbox");}
}
我现在用这个方法实现多选.怎么获得不了它的值呢......

解决方案 »

  1.   

    checkbox 有没有Runat="server" 这个属性呀
      

  2.   

    e.Row.Cells[e.Row.Cells.Count - 1].Text = "<input type='checkbox' runat='server' id='checkbox' value='"+GridView1.DataKeys[e.Row.RowIndex].Value+"'>";这样写是不对的,你这样是动态产生服务器控件,正确的方式我认为你只是写一个列的值不是把这个“<input..>”标识放进去,这个<input 你要写的ASPX页面中,这样的帮定后产可以在CS中FindControl到。
      

  3.   

    看看是不是因为你把checkbox加到GridView里,最后生成出来后checkbox的名字不是叫"checkbox"所以找不到FindControl("checkbox");
      

  4.   

    大哥,实现多选也不用这么麻烦吧,加一个模板列,里面放上CHECKBOX就好了啊你实在要这样做的话,也不能这样直接写HTML进去,.NET是认不到的,因为没有在VIEWSTATES中注册(除非你能够伪造VIEWSTATES)
    用这样动态生成一个控件:
    using (HtmlGenericControl genC = new HtmlGenericControl("DIV"))
                            {
                                genC.ID = "wscDivTipsSystem";
                                genC.Style.Add("font-family", "Times New Roman,Arial, Helvetica, sans-serif");
                                genC.Style.Add("position", "absolute");
                                genC.Style.Add("border", "solid 1px black");                            genC.InnerHtml = "你的HTML";                            try { this.Form.Controls.Add(genC); }
                                catch { this.Page.Controls.Add(genC); }
                            }
      

  5.   

    现在可以用 wscDivTipsSystem 抓到这个控件了
      

  6.   

    模板列加checkbox,很容易的。
      

  7.   

    说没加Runat="server"
    的没有看清代码吧................里面有加.我想我没写清楚吧.........我也知道模版列很容易.我的datacolum全是动态的.你选择可以是一个列也可以是十个列二十个列这个列是不确定的.你用模版列你如何加到最后一个.因为  AutoGenerateColumns是true不是false. 
    所以出来的gridview.column.count是0.我的gridview绑定的是个datatable.
    这个datatable也是动态生成的正如上面所说可以有不限制的1-20列.
      

  8.   

    我自己解决了!!!!
     if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[e.Row.Cells.Count - 1].Text = "<input type='checkbox' runat='server' id='chkItem' value='" + GridView1.DataKeys[e.Row.RowIndex].Value + "'>";
                e.Row.Cells[e.Row.Cells.Count - 1].Attributes.Add("onclick", "document.getElementById('HdnSelectedValues').value='';");
            }
    用的客户端代码解决的。
     function kk()
            {
                for (var i=0;i<document.form1.elements.length;i++)
                    {
                    var e = document.form1.elements[i];
                   if ( (e.type=='checkbox') )
                    {
                    if(e.checked)
                       {
                        document.getElementById('HdnSelectedValues').value+=document.form1.elements[i].value+",";
                       } 
                    }
                    }
             }
    然后给个按纽触发这个客户端事件就好.感谢Knight94.虽然你的代码对我的帮助不大.但很感谢你的帮助.
      

  9.   

    e.Row.Cells[e.Row.Cells.Count - 1].Text = "<input type='checkbox' runat='server' id='checkbox' value='"+GridView1.DataKeys[e.Row.RowIndex].Value+"'>";
    肯定是错误的,你在模板列就可以了