DataList的单条删除功能

<asp:DataList ID="sendtomeList" runat="server" DataKeyField="id" OnItemCommand="sendtomeList_ItemCommand">

<asp:LinkButton ID="LinkButton2" commandname="Delete" runat="server">删除</asp:LinkButton>

 </asp:DataList>

 

 

在DataList的ItemCommand事件中加入:

 

    protected void sendtomeList_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {

            //打开数据库连接
            SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["connStr"]);
            conn.Open();

            string sID = sendtomeList.DataKeys[e.Item.ItemIndex].ToString();//获取id
            SqlCommand comm1 = new SqlCommand("delete from member where id=" + sID, conn);
            comm1.ExecuteNonQuery();
            sendtomeList.DataBind();

            conn.Close();
        }

    }

posted @ 2008-08-24 11:42  missthe  阅读(1084)  评论(0编辑  收藏  举报