DataList批量查出

zhengchengbaishi 2012-04-09 09:06:44
为什么我的代码执行不到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");
}
}

...全文
124 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zongyong 2012-04-10
  • 打赏
  • 举报
回复

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 + ",";
}
}
ycproc 2012-04-09
  • 打赏
  • 举报
回复

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就可以了
zhengchengbaishi 2012-04-09
  • 打赏
  • 举报
回复
不对啊,我选中了呀!
orochiheart 2012-04-09
  • 打赏
  • 举报
回复
if (checkBox.Checked == true) 表示checkBox在界面中是选中的 如果没有执行if块语句 只能是这一种可能 就是界面的checkBox未选中
orochiheart 2012-04-09
  • 打赏
  • 举报
回复
应该是你的checkBox在界面中处于未选中状态的原因
orochiheart 2012-04-09
  • 打赏
  • 举报
回复
好长的messageBox
zhengchengbaishi 2012-04-09
  • 打赏
  • 举报
回复
对不起,标题整错了,是批量删除!

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧