<asp:repeater id="rpt" run="server">
<ItemTemplate>
<asp:LinkButton id="Delete" runat="server" OnCommand="Delete_Command"></asp:linkButton>
<asp:label id="lblMessage" run="server">
</ItemTemplate>
</asp:repeater>
代码背后:
protected void Delete_Command(object sender, CommandEventArgument e)
{
}
如何在Delete_Command中获取对“lblMessage”的引用?
答案 0 :(得分:0)
我建议稍微重新组织您的代码,这样您就可以使用FindControl
轻松执行此类操作。基本上我的建议归结为处理Command事件而不是链接按钮,而是处理转发器本身:
<asp:repeater id="rpt" run="server" OnItemCommand="rpt_ItemCommand">
然后访问项目内容非常简单:
protected void rpt_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
Label label = e.Item.FindControl("lblMessage");
LinkButton linkButton = e.Item.FindControl("Delete");
}
答案 1 :(得分:0)
你可以试试这个:
Page:
<asp:Repeater ID="rpt" runat="server" OnItemCommand="rpt_ItemCommand">
<ItemTemplate>
<asp:LinkButton ID="Delete" runat="server" CommandName="Delete"></asp:LinkButton>
<asp:Label ID="lblMessage" runat="server"/>
</ItemTemplate>
代码背后:
protected void rpt_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
Label lblMessage = e.Item.FindControl("lblMessage") as Label;
}
}