用在源代码区里给gridview1绑定了一个名字叫做 "CBox"的checkbox类型的控件
以下是代码
<asp:GridView ID="GridView1" runat="server" AllowPaging ="True" AutoGenerateColumns ="False" Font-Size="10pt"
         HorizontalAlign ="Center" Width="780px" ShowHeader ="False"  borderwidth="0px">
         <Columns >
          <asp:TemplateField>
                <ItemTemplate>
                <asp:CheckBox ID="CBox" runat="server">
                </asp:CheckBox>
                </ItemTemplate>
         </asp:TemplateField>
 </asp:GridView>
此时可以正确现实出这个checkbox可是选中了这个选框后,在后台用到这个控件的checked返回值得时候却显示为false  bool isChecked = ((CheckBox) row.FindControl("CBox")).Checked;请问怎样解决这个问题呢,谢谢!

解决方案 »

  1.   

    就一个多选按钮为什么要放在gridview里面???
      

  2.   

    看下在 Page_Load中是不是重新绑定GV了
      

  3.   

    Page_Load里的代码是这样写的:protected void Page_Load(object sender, EventArgs e)
        {
            if (inSession() != true)
            {
                Response.Redirect("index.aspx");
            }
            else
            {
                GV();
            }
        }
    应该没什么问题吧
      

  4.   

    绑定过了 是
    protected void Page_Load(object sender, EventArgs e)
        {
            if (inSession() != true)
            {
                Response.Redirect("index.aspx");
            }
            else
            {
                GV();
                GridView1.databind();
            }
        }
      

  5.   

    foreach(GridViewRow gvr in GridView1.Rows)
    {
       CheckBox chk;
       chk=(CheckBox)gvr.FindControl("CheckBox1");
       if(chk.Checked)
      {
         ...
      }
    }
      

  6.   

    用遍历的方法 去循环GridView1中每Rows的CheckBox控件
      

  7.   

    我是用 for 循环查询的     for (int i = 0; i < GridView1.Rows.Count; i++)
        {
           GridViewRow row = GridView1.Rows[i];         
           bool isChecked = ((CheckBox) row.FindControl("CBox")).Checked;
           if (isChecked)
           {
                  .....
           }
         }
      

  8.   

    这个是后台的代码
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
      GridViewRow row = GridView1.Rows[i];         
      bool isChecked = ((CheckBox) row.FindControl("CBox")).Checked;
       if (isChecked)
       {
             .....
       } 
    }     
    前台的html代码是这样的
    <asp:GridView ID="GridView1" runat="server" AllowPaging ="True" AutoGenerateColumns ="False" Font-Size="10pt"
             HorizontalAlign ="Center" Width="780px" ShowHeader ="False"  borderwidth="0px">
             <Columns >
              <asp:TemplateField>
                    <ItemTemplate>
                    <asp:CheckBox ID="CBox" runat="server">
                    </asp:CheckBox>
                    </ItemTemplate>
             </asp:TemplateField>
     </asp:GridView>结果就是只能显示出这个checkbox的框但是不能得到值
      

  9.   

    checkbox 本身你也没有绑定值 当然是没有值的  只能得到控件  然后判断他是否选中你在C#中写在了什么事件里?
      

  10.   

    在一个按钮点击的事件里判断这个checkbox是否被选中的。
    楼上的能不能给提供一个可以试验的代码 谢谢
      

  11.   

    for (int i = 0; i < GridView1.Rows.Count; i++)
        {
           GridViewRow row = GridView1.Rows[i];         
           bool isChecked = ((CheckBox) row.FindControl("CBox")).Checked;
           if (isChecked)
           {
                  .....
           }
         }GridView1.Rows.Count這里好象沒有值,我也郁悶了半天
      

  12.   

    #region//批量删除
        protected void alldelbtn_Click(object sender, EventArgs e)
        {
            conString();
            CheckBox chk;
            foreach (GridViewRow gvr in GridView1.Rows)
            {
                chk = (CheckBox)gvr.FindControl("CheckBox1");
                if (chk.Checked)
                {
                    int id = Convert.ToInt32(GridView1.DataKeys[gvr.RowIndex].Value.ToString());
                    com = new SqlCommand("update authors set delstate=1 where au_id='" + id + "'", con);
                    try
                    {
                        con.Open();
                        com.ExecuteNonQuery();
                        lblmessage.Text = "批量删除成功!";
                        con.Close();
                    }
                    catch (Exception ex)
                    { lblmessage.Text = "无法批量删除!"; }
                }
            }
            PBind();
        }
        #endregion
      

  13.   

    我这运行没问题 到你那就有问题啦?
    是还是false啊 还是取不出KEY值来啊
      

  14.   

    成功了!Thanks,樓主你不要心急,好好看看
      

  15.   

    你加断点看看 到底是什么毛病
    huangzhixiang() 都没问题了吗不是
    你再好好看看
      

  16.   

    就是点过checkbox后它的check属性还是false
      

  17.   


    晕了
    那你看看 chk=(CheckBox) row.FindControl("CBox");
    chk有没有被实力化啊?
      

  18.   

    chk不是问题的根源 ((CheckBox) row.FindControl("CBox")).check就是false
      

  19.   

    protected void Audi_btn_Click(object sender, EventArgs e)
        {        CheckBox chk;
            foreach (GridViewRow gvr in GridView1.Rows)
            {
                chk = (CheckBox)gvr.FindControl("CBox");
                if (chk.Checked)
                {
                    int id = Convert.ToInt32(GridView1.DataKeys[gvr.RowIndex].Value.ToString());
                    com = new SqlCommand("update authors set delstate=1 where au_id='" + id + "'", con);
                    try
                    {
                        SqlCommand sqlcmd = new SqlCommand();
                        sqlcmd.CommandText = "update MessageBoard set isnew='false' where id='" + GridView1.Rows[i].Cells[1].Text + "'";
                        ADOConnectionClass.ConnectingwithoutReturn(sqlcmd);
                        sqlcmd.Dispose();
                    }
                    catch (Exception ex)
                    {
                        sqlcmd.Dispose();
                    }
                }
            }
      

  20.   

    只能显示出这个checkbox的框但是不能得到值 为什么呢??前台:
    <asp:GridView ID="GridView1" runat="server" AllowPaging ="True" AutoGenerateColumns ="False" Font-Size="10pt"
             HorizontalAlign ="Center" Width="780px" ShowHeader ="False"  borderwidth="0px">
             <Columns >
              <asp:TemplateField>
                    <ItemTemplate>
                    <asp:CheckBox ID="CBox" runat="server">
                    </asp:CheckBox>
                    </ItemTemplate>
             </asp:TemplateField>
     </asp:GridView>
    后台:
    protected void Audi_btn_Click(object sender, EventArgs e)
        {        CheckBox chk;
            foreach (GridViewRow gvr in GridView1.Rows)
            {
                chk = (CheckBox)gvr.FindControl("CBox");
                if (chk.Checked)
                {
                    try
                    {
                        SqlCommand sqlcmd = new SqlCommand();
                        sqlcmd.CommandText = "update MessageBoard set isnew='false' where id='" + GridView1.Rows[i].Cells[1].Text + "'";
                        ADOConnectionClass.ConnectingwithoutReturn(sqlcmd);
                        sqlcmd.Dispose();
                    }
                    catch (Exception ex)
                    {
                        sqlcmd.Dispose();
                    }
                }
            }Page_Load里的代码是这样写的:protected void Page_Load(object sender, EventArgs e)
        {
            if (inSession() != true)
            {
                Response.Redirect("index.aspx");
            }
            else
            {
                GV();
                GridView1.databind();
            }
        }结果就是只能显示出这个checkbox的框但是不能得到值
      

  21.   

    if (inSession() != true)
    {
          Response.Redirect("index.aspx");
    }
    else
    {
          GV();
          GridView1.databind();
    }
    改成
    if(!IsPostBack)
    {
    if (inSession() != true)
    {
          Response.Redirect("index.aspx");
    }
    else
    {
          GV();
          GridView1.databind();
    }
    }
    试试?
      

  22.   

    try
                    {
                        SqlCommand sqlcmd = new SqlCommand();
                        sqlcmd.CommandText = "update MessageBoard set isnew='false' where id='" + GridView1.Rows[i].Cells[1].Text + "'";//整个gridview就只有一GridView1.Rows[i].Cells[1].Text肯定不回有值了
                        ADOConnectionClass.ConnectingwithoutReturn(sqlcmd);
                        sqlcmd.Dispose();
                    }
                    catch (Exception ex)
                    {
                        sqlcmd.Dispose();
                    }
      

  23.   


    if (inSession() != true)
    {
          Response.Redirect("index.aspx");
    }
    else
    {
          GV();
          GridView1.databind();
    }
    改成
    if (inSession() != true)
    {
          Response.Redirect("index.aspx");
    }
    else
    {
    if(!IsPostBack)
    {      GV();
          GridView1.databind();
    }
    }
    我觉得应该这样改才正确
      

  24.   

    protected void Page_Load(object sender, EventArgs e)
        {
            if (inSession() != true)
            {
                Response.Redirect("index.aspx");
            }
            else
            {
                if(!IsPostBack)
                {
                    GV();
                    GridView1.databind();
                }
            }
        }
      

  25.   

    checbox的 autopostback属性设置为 true.
      

  26.   


    protected void Page_Load(object sender, EventArgs e)
        {
            if (inSession() != true)
            {
                Response.Redirect("index.aspx");
            }
            else
            {
                if(!IsPostBack)
                {
                    GV();
                    GridView1.databind();
     this.GridView1.DataKeyNames = new string[] { "mm_ID" };
    //读该行的主键值!!
                }
            }
        }
      

  27.   

    批量可以考虑js
    前台:
     <script type="text/javascript" language="javascript">
        
        function SelectAllCheckboxes(spanChk){
         var oItem = spanChk.children;
         var theBox=(spanChk.type=="checkbox")?spanChk:spanChk.children.item[0];
         xState=theBox.checked;
         elm=theBox.form.elements;
         for(i=0;i<elm.length;i++)
         if(elm[i].type=="checkbox" && elm[i].id!=theBox.id)
         {
          if(elm[i].checked!=xState)
          elm[i].click();
         }
        }
    </script>
        <asp:TemplateField HeaderText="全选">
        <ItemTemplate>
         <asp:CheckBox ID="chkSelect" runat="server" />
         </ItemTemplate>
           <HeaderTemplate>
        <input id="chkAll" onclick="javascript:SelectAllCheckboxes(this);" runat="server"  type="checkbox" />全选
     </HeaderTemplate>
    </asp:TemplateField>
    后台,当然你也可以不使用全选,可以随便点几个
      protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (Session.Contents["Control"].ToString() == "N")
                {
                    Response.Write("<script language='javascript'>alert('您尚未分配该权限,请与管理员联系!');</script>");
                    return;            }
                else
                {
                    int temp = 0;
                    dbm_cp_dal dal = new dbm_cp_dal();
                    for (int i = 0; i < gdv_CP.Rows.Count; i++)
                    {
                       
                        GridViewRow row = gdv_CP.Rows[i];
                        string ctlname = "chkSelect";
                        Control mycontrol = row.FindControl(ctlname);
                        bool isChecked = ((CheckBox)mycontrol).Checked;
                        if (isChecked)
                        {
                           
                            temp = dal.Delete("where CPID=" + System.Convert.ToInt32(row.Cells[1].Text));
                                               }
                       
                    }
                    if (temp >= 1)
                    {
                        Response.Write("<script language='javascript'>alert('删除数据成功!');</script>");
                        dal = null;
                        GridViewBind();
                       
                    }
                    else
                    {
                        Response.Write("<script language='javascript'>alert('请先选择要删除的数据!');</script>");
                        return;
                    }
                }        }
            catch
            {
                Response.Write("<script language='javascript'>alert('数据删除失败!');</script>");
                return;
            }    }
      

  28.   

    结帖了。谢谢 大家 的 帮助。找到问题了 pageload里面写上 if(!postback){GV();}就ok了。
    答案里面有上面这句话的都有分。
      

  29.   

    特别感谢zwwlovezy1  回帖比较勤 虽然解决的不是我的问题也谢谢 赠5分