<asp:GridView ID="gvFun" runat="server" 
        GridLines="None" AutoGenerateColumns="False" DataKeyNames="f_Id,f_Name">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="chkFun" runat="server" />
                </ItemTemplate>       
            </asp:TemplateField>
      
        </Columns>
    </asp:GridView>  private void AdFunDelete()
    {
        try 
        {
            for (int i = 0; i < this.gvFun.Rows.Count; i++)
            {
                CheckBox cbFun = (CheckBox)this.gvFun.Rows[i].FindControl("chkFun");
                if (cbFun.Checked == true) //error
                {

                }
            }
        }
        catch
        {
        }
    }
在error处总是返回false,但是CheckBox已经选中了,请问是什么问题?

解决方案 »

  1.   

    <asp:CheckBox   ID="chkFun"   runat="server"  AutoPostBack="True"  /> 
      

  2.   

     CheckBox   cbFun   =   (CheckBox)this.gvFun.Rows[i].FindControl("chkFun"); 
     要不就是这个返回值为false,有没有NAME=chkFun的控件啊
      

  3.   

    要保证这个函数AdFunDelete()执行之后再进行GridView的绑定!
      

  4.   

    还有if   (cbFun.Checked)   //error
    就可以了。 
      

  5.   

     protected void btnDelte_Click(object sender, EventArgs e)
        {
            //对有没有选中进行判断直接按删除按钮则报错
            bool strDrive = false;
            try
            {
                SQLT.SetSQLBeginTrans();
                for (int i = 0; i < dlHuanzhe.Items.Count; i++)
                {
                    CheckBox CB = (CheckBox)dlHuanzhe.Items[i].FindControl("chk");                if (CB.Checked)
                    {
                        strDrive = true;
                        //隐藏传递ID用一个datatlist就是Parent一个
                        string delID = ((HiddenField)dlHuanzhe.Items[i].FindControl("id")).Value;
                        SQLT.Operate("delete from dbo.F_User where  userid='" + delID + "'");
                    }
                }            SQLT.SetSQLCommitTrans();
                SQLT.ConClose();
            }
            catch
            {
                SQLT.SetSQLRollBack();
                Response.Write("<script>alert('删除失败。')</script>");
                SQLT.ConClose();
            }
            if (!strDrive)//判断选中没有
            {
                Response.Write("<script>alert('请选择要删除的数据。')</script>");
            }
            else//删除后重新绑定
            {
      
                this.BindShowF_Patient();        }
        }
      

  6.   

    绑定是放在
    if(!this.IsPostback) 

        Bind(); 
    }
    里的。
    肯定没有重新绑定阿 
      

  7.   

    楼主可以这样试下://提交当前复选框的选择状态
                    private   void   dataGridView_CurrentCellDirtyStateChanged(object   sender,   EventArgs   e) 
                    { 
                            this.dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit); 
                    } 
    如果问题得到解决的话,hoho...,分拿来!