我有一个Repeater
,对于Repeater
中的每个项目,我都有一个按钮来触发CommandEvent。当我的按钮被点击时,我需要传递一个对象作为参数。
你能帮我解决这个问题吗?
处理Repeater的ItemCommand并将对象作为CommandArgument传递。
下面是一个例子。
<asp:LinkButton ID="btnDeleteComment" runat="server" Text="Delete" CommandName="DeleteComment" CommandArgument=<%#Eval("CommentID") %>></asp:LinkButton>
码尾
protected void rptComments_ItemCommand(object source, RepeaterCommandEventArgs e) {
if(e.CommandName.ToLower().Equals("deletecomment")) {
clsComment comment = new clsComment("mediadb");
comment.CommentID = int.Parse(((LinkButton)e.CommandSource).CommandArgument);
comment.DeleteRecord();
rptComments.DataBind();
}
}
CommandArgument
将作为字符串传递,因此请确保您首先转换了它,以避免可能错误的隐式转换。
您应该使用类的键参数,例如ID。如果需要实例,请在ItemCommand
事件处理程序中创建它。
它是什么类型的对象?按钮具有CommandArgument属性。您可以在其中保存字符串值。