服务器控件后台获取checkbox选取的值,获取不到值
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.txtResult.Text = string.Empty;
        if (grid.Rows.Count > 0)
        {
            foreach (GridViewRow row in grid.Rows)
            {
                HtmlInputCheckBox _chkItem = (HtmlInputCheckBox)row.FindControl("chkItem");
                if (_chkItem != null && _chkItem.Checked)
                {
                    this.txtResult.Text += _chkItem.Value + ",";
                }
            }
            if (this.txtResult.Text.Length > 0)
            {
                this.txtResult.Text = this.txtResult.Text.Substring(0, this.txtResult.Text.Length - 1);            }
        }请教大虾代码哪里有问题

解决方案 »

  1.   

    foreach (GridViewRow dr in GridView1.Rows)
    {
    CheckBox chk= (CheckBox)gvr.FindControl("CheckBox1");
       if (chk!=null&& chk.Checked)
       {
    GridViewRow row = chk.NamingContainer as GridViewRow;
    string s=dr.Cells[0].Text;}
      

  2.   

    把HtmlInputCheckBox _chkItem = (HtmlInputCheckBox)row.FindControl("chkItem");
    改成
    CheckBox _chkItem = (CheckBox)row.FindControl("chkItem");
    试试
      

  3.   

    HtmlInputCheckBox _chkItem = (HtmlInputCheckBox)row.FindControl("chkItem");debug一下,这个对象是null吗?
      

  4.   

    debug调试一下看看row.FindControl("chkItem")取到的是什么类型
      

  5.   

    你是写在Button1_Click里面,会不会GridView里还没绑定数据,一行也没有。
      

  6.   

    断点看了 进入if了 有记录,但是执行到chk,chk一直为空
      

  7.   


                            <asp:GridView ID="grid" runat="server" AutoGenerateColumns="False" OnPreRender="grid_PreRender">
                    <Columns>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                <input type="checkbox" id="chkAll" />
                            </HeaderTemplate>
                            <ItemTemplate>
                                <input type="checkbox" id="chkItem" value='<%# Eval("LOT_NB") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="LOT_NB" HeaderText="LOT_NB" />
                        <asp:BoundField DataField="ORD_DT" HeaderText="ORD_DT" />
                        <asp:BoundField DataField="COMP_DT" HeaderText="COMP_DT" />
                        <asp:BoundField DataField="BASELOC_CD" HeaderText="BASELOC_CD" />
                    </Columns>
                </asp:GridView>页面代码
      

  8.   

    有的 调试的时候 grid.Rows.Count=7
      

  9.   

    <input type="checkbox" id="chkItem" value='<%# Eval("LOT_NB") %>' runat='server' />
      

  10.   

    如果id不对,或者类型转换错误,那么这句
     HtmlInputCheckBox _chkItem = (HtmlInputCheckBox)row.FindControl("chkItem");
    应该会报“未将对象引用到”错误,但你的却没有报错,这说明id和类型转换是对的。单击按钮时先执行Page_Load方法,是不是把GridView里的东西又给初始化了?
      

  11.   

    看了11楼贴出的代码才知道,你的复选框没有加runat="server",所以不是html控件。
      

  12.   

    谢谢大胡子 就是这个问题?为什么要加 runat='server'呢