在Command执行时访问与RepeaterItem关联的数据

时间:2009-05-20 06:33:45

标签: asp.net events repeater

我想访问与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

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:1)

根据Mitchel的说法,确保检查RowType是否为DataRow。当你不能做时,不要做垃圾。从e.Item.DataItem到你的类型的转换会在页眉或页脚行上失败。