我看到您正在使用中继器,因此您可能可以使用以下代码:
在您的转发器模板中:
<asp:Repeater ID="_postsRepeater" runat="server" OnItemCommand="_postsRepeater_ItemCommand">
<ItemTemplate><asp:LinkButton ID="_postDeleteLinkButton" runat="server" CommandName="DeletePost" CommandArgument="<%# ((Post)Container.DataItem).ID %>">Delete</asp:LinkButton></ItemTemplate>
</asp:Repeater>
然后处理转发器的 ItemCommand 事件:
protected void _postsRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "DeletePost") // Replace DeletePost with the name of your command
{
// Get the passed parameter from e.CommandArgument
// e.g. if passed an int use:
// int id = Convert.ToInt32(e.CommandArgument);
}
}