1

我想访问与 ItemCommand 在其中触发的 RepeaterItem 关联的数据。场景是,我有多个RepeaterItems,其中Button控件以声明方式设置Command,如下所示:

<asp:Repeater ID="Repeater3" 
              runat="server" 
              DataSource='<%# ClientManager.GetClientEmployees(Eval("ClientID")) %>' 
              OnItemCommand="RemoveEmployeeFromClient">
    <ItemTemplate>
        <asp:LinkButton ID="LinkButton1" 
                        runat="server" 
                        Text="(x)" 
                        CommandName="RemoveEmployeeFromClient">
        </asp:LinkButton>
    </ItemTemplate>

    <SeparatorTemplate>,<br /></SeparatorTemplate>
</asp:Repeater>

后面的代码是:

Protected Sub RemoveEmployeeFromClient(ByVal source As Object, 
                                       ByVal e As RepeaterCommandEventArgs)
    ' I want to access the data associated with 
    ' the RepeaterItem which the Button was clicked.
End Sub
4

2 回答 2

4

您可以使用 e.Item.DataItem 来获取对象的数据,也可以将其存储在隐藏字段中。

于 2009-05-20T06:48:10.520 回答
1

在 Mitchel 所说的基础上,确保检查 RowType 是否为DataRow。做不到的时候不要做废话。从 e.Item.DataItem 到您的类型的转换将在页眉或页脚行上失败。

于 2009-05-20T06:52:11.217 回答