<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand" DataKeyField="guidanceid"> 
        <ItemTemplate>
            <%# Eval("guidancetitle")%><%# Eval("userid")%><%# Eval("mailbox")%><%# Eval("updatetime")%>
            <asp:Button ID="settop" runat="server" Text="置顶" CommandName="settop" /><asp:Button ID="sethot" runat="server" Text="热点问题" CommandName="sethot" />
            <asp:Button ID="answer" runat="server" Text="回复" CommandName="answer" /><asp:Button ID="del" runat="server" Text="删除" CommandName="del" />
        </ItemTemplate>
        </asp:DataList>
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        binddata();
    }
    protected void binddata()
    {
        ......
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        int id = System.Convert.ToInt32(ds.Tables[0].Rows[0]); //id取数据库中的guidanceid字段(第一个字段)
        if (e.CommandName == "del")
        {
            SqlConnection cn = new SqlConnection("server=localhost; database=gra_design; user=sa; password=;");
            SqlCommand cmd = new SqlCommand("delete from guidance where guidanceid=" + id, cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
            binddata();
        }怎么按del按钮没有反应呢?数据库里的数据并没有被删除

解决方案 »

  1.   

    int id = System.Convert.ToInt32(ds.Tables[0].Rows[0]); 这句话不对吧,既然你要取得是id
    你应该取出来啊,你这句话只取出一行数据,并不是一个数据
    改成这样int id = System.Convert.ToInt32(ds.Tables[0].Rows[0]["guidanceid"].ToString());注意红色的部分
      

  2.   

    int id = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
      

  3.   

    我在DataList1_ItemCommand事件中设断点,但是按删除button没有任何反应,似乎是没有触发这个事件
    真是很奇怪
      

  4.   

    我在DataList1_ItemCommand事件中设断点,但是按删除button没有任何反应,似乎是没有触发这个事件 
      

  5.   

    正常情况下,删除按钮的CommandName应该是Delete,然后在DataList的DeleteCommand事件中处理删除操作