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

时间:2022-04-24 15:53:53

I want to access the data associated with the RepeaterItem in which an ItemCommand fired up. The scenario is, I have multiple RepeaterItems which Button controls in which the Command is set declaratively like this:

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

The code behind is:

背后的代码是:

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 个解决方案

#1


You can use e.Item.DataItem to get down to the data for the object, or you could store it in a hidden field.

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

#2


Building on what Mitchel said, make sure you check to see that the RowType is DataRow. Don't want to do crap when you can't. The cast from e.Item.DataItem to your type would fail on the header or footer row.

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

#1


You can use e.Item.DataItem to get down to the data for the object, or you could store it in a hidden field.

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

#2


Building on what Mitchel said, make sure you check to see that the RowType is DataRow. Don't want to do crap when you can't. The cast from e.Item.DataItem to your type would fail on the header or footer row.

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