首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

DataList批量查出解决方案

2012-04-25 
DataList批量查出为什么我的代码执行不到if里面(粉红色部分)去呢?总是删除不了。C# codeasp:DataList ID

DataList批量查出
为什么我的代码执行不到if里面(粉红色部分)去呢?总是删除不了。

C# code
  <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)                {                    [color=#FF00FF]string strID = achievement.DataKeys[i].ToString();                    string sqlStr = "delete from achievements where ach_id='" + strID + "'";                    gwolf_bll.action1(sqlStr);[/color]                }            }            Response.Redirect(Request.Url.ToString());            bind();        }        else        {            DialogResult.Cancel.Equals("true");        }    }


[解决办法]
C# code
  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; 


[解决办法]

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 + ",";
}
}

热点排行