html 代码 
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
   AllowSorting="True" AutoGenerateColumns="False"  
Width="366px" CellPadding="4" 
   ForeColor="#333333" GridLines="None">
 <Columns>
   
   <asp:BoundField DataField="id" HeaderText="ID" 
         InsertVisible="False" ReadOnly="True" />
   <asp:BoundField DataField="tag" HeaderText="标志"  />
   <asp:TemplateField HeaderText="Select">
    <ItemTemplate>
       <asp:CheckBox ID="chkSelect" runat="server" />
    </ItemTemplate>
    <HeaderTemplate>
    </HeaderTemplate>
   </asp:TemplateField> </Columns> <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
 <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
 <PagerStyle BackColor="#FFCC66" ForeColor="#333333" 
                           HorizontalAlign="Center" />
 <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
 <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
 <AlternatingRowStyle BackColor="White" />
</asp:GridView>
        <br />
        <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="审核" /></div>
cs代码
 protected void Button3_Click(object sender, EventArgs e)
    {
        int i;
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["constr"]);
        con.Open();
        for (i = 0; i < GridView1.Rows.Count;i++ )
        {
            
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("chkSelect");
            if (cbox != null)
            {                string uid = GridView1.Rows[i].Cells[0].Text.ToString();
                string cmdtext1 = "update table1 set tag='" + "5" + "' where id=" + uid;
                SqlCommand cmd2 = new SqlCommand(cmdtext1, con);
                cmd2.ExecuteNonQuery();
            }
        }
        GridView1.EditIndex = -1;
        BindData();
    }
点button3的时候,在gridview上的数据全部都更新了,我的要求是更新checkbox被选上的行,帮忙看看是什么原因,谢谢

解决方案 »

  1.   

     if (cbox.Checked) 
      

  2.   


                foreach (GridViewRow gr in GridView1.Rows)
                {
                    CheckBox cbox = (CheckBox)gr.Cells[4].FindControl("chkSelect");  
                    if (cbox.Checked) 
                    {
                        string uid = GridView1.Rows[i].Cells[0].Text.ToString(); 
                        string cmdtext1 = "update table1 set tag='" + "5" + "' where id=" + uid; 
                        SqlCommand cmd2 = new SqlCommand(cmdtext1, con); 
                        cmd2.ExecuteNonQuery(); 
                    }
                }
      

  3.   

    if (cbox != null) 这个改 if(cbox.Checked)
      

  4.   

    还是没有反应啊,if (cbox != null) 这个改成 if(cbox.Checked)都没有执行不了IF里面的语句!晕
      

  5.   

    还是没有反应啊,把if (cbox != null) 这个改 成if(cbox.Checked)后都执行不了IF后面的语句了!