为什么我的代码执行不到if里面(粉红色部分)去呢?总是删除不了。
  <asp:DataList ID="achievement" runat="server" Height="200px" Width="512px">
                <ItemTemplate>
                    <table style=" border-bottom-style:inherit; border:1px;">
                        <tr>
                            <td style=" width:5px;">
                                <asp:CheckBox ID="CheckBox1" runat="server" />
                            </td>
                            <td style=" width:15px;">
                                <asp:Label ID="id" runat="server" Text='<%#Eval("ach_id") %>'></asp:Label>
                            </td>
                            <td style=" width:140px;">
                                <asp:Label ID="pname" runat="server" Text='<%#Eval("project_name") %>'></asp:Label>
                            </td>
                            <td style=" width:65px;"> 
                                <asp:Label ID="Label1" runat="server" Text='<%#Eval("deveolpers") %>'></asp:Label>
                            </td>
                            <td style=" width:140px;">
                                <asp:Label ID="Label2" runat="server" Text='<%#Eval("explanation") %>'></asp:Label>......
                            </td>
                            <td style=" width:25px;">
                                <asp:LinkButton ID="Linbutton1" runat="server" Text="编辑" CommandArgument='<%#Eval("ach_id") %>' OnClick="Modifty_Click"></asp:LinkButton>
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:DataList>
            <asp:Button ID="del" runat="server" Text="删除" OnClick="del_Click" />
            protected void del_Click(object sender, EventArgs e)
    {
        DialogResult result = MessageBox.Show("请确定是否要删除选中的记录行", "友情提示!", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.ServiceNotification);
        if (result == DialogResult.OK)
        {
            for (int i = 0; i < achievement.Items.Count; i++)
            {
                System.Web.UI.WebControls.CheckBox checkBox = ((System.Web.UI.WebControls.CheckBox)((DataListItem)achievement.Controls[i]).FindControl("CheckBox1"));
                if (checkBox.Checked == true)
                {
                    string strID = achievement.DataKeys[i].ToString();
                    string sqlStr = "delete from achievements where ach_id='" + strID + "'";
                    gwolf_bll.action1(sqlStr);

                }
            }
            Response.Redirect(Request.Url.ToString());
            bind();
        }
        else
        {
            DialogResult.Cancel.Equals("true");
        }
    }

解决方案 »

  1.   

    应该是你的checkBox在界面中处于未选中状态的原因
      

  2.   

       if (checkBox.Checked == true) 表示checkBox在界面中是选中的 如果没有执行if块语句 只能是这一种可能 就是界面的checkBox未选中
      

  3.   


      foreach (Datalist item in this.datalist.Items)
            {
                CheckBox chkItem = (CheckBox)item.FindControl("ChkBox");
                if (chkItem.Checked)
                {
                    //被勾选
                    ids += chkItem.ToolTip + ",";
                }
            }
     if (ids != "")
            {
                ids = ids.Substring(0, ids.Length - 1);
            }
            return ids;
    然后后面删除时间的时候调用这个方法,只需要在SQL语句中 delete from table where id 这里用in就可以了
      

  4.   


    1、<ItemTemplate>
                        <table style=" border-bottom-style:inherit; border:1px;">
                            <tr>
                                <td style=" width:5px;">
                                    <asp:CheckBox ID="CheckBox1" runat="server" />
                                </td>
    <td style=" width:65px;"> 
                                    <asp:Label ID="Label1" runat="server" Text='<%#Eval("deveolpers") %>'></asp:Label>
                                </td>
    .......
                       </table>
       </ItemTemplate>为什么每一项生成一个 table,这样做不好吧; 2、这样写是可以的:foreach (DataListItem item in DataList1.Items)
                {
                    var ck = (CheckBox)item.FindControl("CheckBox1");
                    if (ck.Checked)
                    {
                        var lab = (Label)item.FindControl("id");
                        ids += lab.Text + ",";
                    }
                }