datalist的删除无法实现

2024-12-28 07:38:38
推荐回答(2个)
回答1:

你把LinkButton换成Button试试
设置DataList的DataKeyField=主键,Button的CommandName="Delete"
在DataList的ItemCommand的事件里面:
ItemCommand:
if (e.CommandName=="Delete")
{
int ID =(int)DataList.DataKeys[e.Item.Itemindex]
//int就是要删的主键,删除这条纪录就行了
}

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
int id = (int)DataList1.DataKeys[e.Item.ItemIndex];
Response.Write(id);
}
}

回答2:

//删除事件
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.Item.ItemIndex != -1 && e.CommandName == "RemoverZD")//判断索引和CommandName
{
int id = int.Parse(e.CommandArgument.ToString());//获取激发事件时的行的主键ID
TradeManager man = new TradeManager();
if (man.DeleteTrade(id))//调用的删除方法
{
Page.RegisterClientScriptBlock("alert", "");
initTrade();
}
}
}