从Repeater传递参数

时间:2012-01-17 18:05:57

标签: c# asp.net

我有Repeater,对于Repeater中的每个项目,我都有一个按钮来触发CommandEvent。我需要在单击按钮时将对象作为参数传递。

你能帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

处理转发器的ItemCommand并将对象传递为CommandArgument

Here就是一个例子。

<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将作为字符串传递,因此请确保您首先将其转换为避免错误的隐式转换。

您应该使用班级的key-parameter,例如ID。如果您需要该实例,请在ItemCommand event-handler。

中创建它

答案 1 :(得分:1)

它是什么样的物体? Button具有CommandArgument属性。您可以在其中保存字符串值。