看看其中的三个事件 ItemCommand、ItemCreated、ItemDataBound
要做删除事件主要是用ItemCommand,它会引发CommandEvent事件
ItemCreated的事件是在控件初始化加载时能加载些什么,如果要注册一些提示脚本和样式等信息在这里写就可以
我曾经遇到过用SESSION判断其中的按钮是不是可用,就写在这里面了.
ItemDataBound是在数据绑定后激发,同样可以配合着第一种事件做删除等操作
第一步:在DataList里加入DatakeyName属性
<asp:DataList ID="DataList1" runat="server"
DataKeyField="RemarkID" Width="100%" >
</asp:DataList>
这里的RemarkID是从数据库中绑定的主键的字段名
第二步:在DataList的模板中放入一个BUTTON,这里用LINKBUTTON做,并加上commandname属性
<asp:DataList ID="DataList1" runat="server"
DataKeyField="RemarkID" Width="100%" >
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" commandname = "DRdel" runat="server">删除</asp:LinkButton>
</ItemTemplate>
</asp:DataList>
第三步:加上上面提到的事件
<asp:DataList ID="DataList1" runat="server" DataKeyField="RemarkID" Width="100%"
OnItemCommand="DataList1_ItemCommand" >
然后就是在事件中添加信息
protected
void
DataList1_ItemCommand(
object
source, DataListCommandEventArgs e)
![](/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...
{
if (e.Item.ItemIndex != -1 && e.CommandName == "DRdel")//判断索引和CommandName
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
int id = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex].ToString());//获取激发事件时的行的主键ID
FCD_Remark fcd_Remark = new FCD_Remark();//这是自定义类
if (fcd_Remark.Delete(id))//调用的删除方法
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
Lb_message.Text = "删除评论成功";
BindRemark();
}
else
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
Lb_message.Text = "删除评论失败";
}
}
}
![](/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
上面的自定义类各位完全可以自己换成自己的删除方法
如果配合其他的事件还可以添加这样的效果:
![](/syntaxhighlighting/OutliningIndicators/None.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
![](/syntaxhighlighting/OutliningIndicators/None.gif)
![](/syntaxhighlighting/OutliningIndicators/None.gif)
![](/syntaxhighlighting/OutliningIndicators/None.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
需要注意的是,在DATALIST中定义的控件是不能直接在代码窗体中找到的,必须用FindControl方法才能找到
Trackback: [url]http://tb.blog.csdn.net/TrackBack.aspx?PostId=1903034[/url]