如何在后面的代码中获取对转发器项中标签的引用 [英] How to get Reference to the label in repeater item in code behind

查看:43
本文介绍了如何在后面的代码中获取对转发器项中标签的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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"的引用.

how i get the reference to the "lblMessage" in Delete_Command.

推荐答案

试试这个:

protected void Delete_Command(object sender, CommandEventArgs e)
{
    LinkButton button = (LinkButton)sender;
    Label label = (Label)button.NamingContainer.FindControl("lblMessage");
    // do something with the label
}

如果你:

  1. 已绑定中继器
  2. 启用 ViewState
  3. 不要在回发之前重新绑定转发器

这应该有效.如果不是,请验证标签的 id 确实与 ...FindControl("lblMessage"); 中的完全相同.还要确保在所有涉及的控件上设置了 runat="server".

this should work. If not, please verify that the id of the label is indeed exactly the same as in the ...FindControl("lblMessage");. Also make sure that runat="server" is set on all the controls involved.

编辑:还要检查一件事:搜索标记文件(.aspx 文件)并检查是否有任何其他控件在其中使用相同的事件后面的代码.如果另一个控件正在使用相同的事件处理程序并且该控件不在转发器中,则将找不到该标签.

Edit: One more thing to check: Search the markup file (the .aspx file) and check if there are any other controls that also use the same event in the code behind. If another control is using the same event handler and that control is not in the repeater, the label will not be found.

这篇关于如何在后面的代码中获取对转发器项中标签的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆